Syntax error during variable import and assignment [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
When I run
import CardNumbers = "6045781112112478"
the interpreter says there's a SyntaxError and highlights the = sign. What else would I put there?

The syntax error arises because you're mixing two separate Python functionalities in one line: importing and variable assignment.
You can import a module defined elsewhere using Python's import machinery:
import CardNumbers
Or you can assign a variable using the assignment operator =:
CardNumbers = "6045781112112478"
I recommend that you determine which one of those things is your objective in the context of your code.

So are you trying to create a variable named CardNumbers with an integer value of 6045781112112478 because if so you shoudnt put the import statement at the start of the line of code

If you want to use DoodleVibs solution then you have to define where from you want to import CardNumbers by typingimport CardNumbers from WhereEver where WhereEver is the file you want to import CardNumbers from. I hope this helps

Related

Is indirect recursion possible in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I tried searching online but did not find any answer to this particular question. In python there are no function declarations and a function can't be used until it has been defined.
Does this mean that indirect recursion is impossible in python?
And is there a way around this by using some modules?
No, it is possible
def f():
print('from f')
g()
def g():
print('from g')
f()
"a function can't be used until it has been defined" is not so straightforward. When the code runs, the name of the objects that it refers to have to exist. So, you can't do
f()
def f():...
because f() actually executes something. But definitions create a function object, without running it at the time. In the example, the function is claled at the last line of the script, and, by that time, both f, g do exist.

Using a function argument in a dataframe variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm a novice python programmer and thinking this is a very simple task.
I'm trying to use a function argument as a value within a df variable when calling the function, but it is returning the argument address and not the argument value.
def func_name(var_name):
df['varname']=str(var_name)
func_name(split_rand)
df
I want varname to contain "split_rand" throughout, but it contains <function split_rand at 0x0000025E4EAD9A60>. I know that enclosing 'split_rand' in quotes will work, but I don't want to use that for alternative reasons.
Thank you
It's returning the string representation of that function name. If you want the actual function name, then you do func.__name__
I think you meant to do df['varname']=var_name.__name__ instead and I'd rename var_name to func.

Re-usable function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)

Accessing a function variable from another file in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have two files File1 and File2. In File1 I have a function detect() defined as:
def detect():
s = // some operations
I need to access this variable s in File2.
I have tried declaring the variable as global but it has not worked.
How can I access the variable without creating a class as in this post or by using __main__ as in this post ??
function detect must be run to init its local variables.
def detect():
detect.tmp = 1
def b():
print(detect.tmp)
detect()
b()
of course you can import one python file as python module and call its functions like
from File1 import detect
print(detect.tmp)

Why must we give the module name directly to the `import` statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading https://stackoverflow.com/a/28231805/156458
Why can we give the module name as a variable to builtins.__import__ function,
while we must give the module name directly to the import statement?
What difference between a statement and a function that leads to such a difference?
The same reason you need to give the name of the function to the def statement, or the name of the class to class; both can be created by other means too. Because that's how the syntax is designed, to make common actions possible with readable clear, concice syntax.
For the same reason you use object.attributename when you already know the attribute name you want to access, but use getattr() when you need to determine the name dynamically. The latter is more work for the programmer to understand.
Imagine having to wade through
modules_to_import = ['builtins', 'datetime']
for name in modules_to_import:
globals()[name] = __import__(name)
print_function_name = 'print'
current_time = getattr(getattr(globals()['datetime'], 'datetime'), 'now')()
getattr(globals()['builtins'], print_function_name).__call__('Hello world!, it is now', current_time)
That'd be totally unreadable. Compare that to
import datetime
current_time = datetime.datetime.now()
print('Hello world!, it is now', current_time)
Python is a highly dynamic programming language, however, so you are given tools to do many tasks dynamically. type() lets you build classes dynamically, functions can be constructed without using def or lambda, and you can import modules based on a variable. You use those tools when you have a clear use-case for dynamic behaviour, rather than static (but more readable) syntax.

Categories