Why python want us this : Non-default argument follows default argument [closed] - python

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 9 months ago.
Improve this question
class Node():
def __init__(self,value,parrent=None,neigh) -> None:
self.val=value
self.parrent=parrent
self.neigh=neigh
Here I want to define a class. There is an error about neigh that non-default argument follows default argument. I saw the solution of this question but my main question is I want to know why python want us to do this?

Because Python allows omission of keyword / default-specified parameters and allows passing parameters without explicitly naming them. If your definition was legal syntax (and the rest of the language functioned the same way), then instantiating
n = Node(4, 6)
might mean either
n = Node(value=4, parrent=6, neigh=None)
or
n = Node(value=4, parrent=None, neigh=6)
The point of syntax rules is to resolve ambiguities like this.

Related

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.

Passing a string to eval in method [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 2 years ago.
Improve this question
Struggling with passing a variable reference to a nested function. Using a dictionary is not an option in my use case. It's a much simplified MRE (real use passes an object with many nested objects).
def func(reference):
eval('trueVal=' + reference)
print(trueVal) #Expecting trueVal=15000
trueValue = 15000
reference = 'trueValue'
func(reference)
eval evaluates expressions. The result of your expression in your example can then be assigned to trueVal explicitly:
trueVal = eval(reference)
I would not endorse using eval or exec, 99 times out of 100, there is a better way to do it, dictionary is not the only option but without posting your question its impossible to provide a better way to approach it. below is for reference as an example that works without hardcoding the variable name. But really there is always likely a better approach thatn eval or exec.
def func(reference, value):
exec(reference + '="' + str(value) +'"')
print(reference, ":", eval(reference)) #Expecting trueVal=15000
trueValue = 15000
reference = 'trueVal'
func(reference, trueValue)

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)

According to style is it right to put a funcion like parameter when ending an aplication with sys.exit() 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 6 years ago.
Improve this question
I have seen a code written like this:
if __name__ == "__main__":
#...
result = someFunction(someParameter)
sys.exit(result)
and even like this, where you write the call of the function in the sys.exit() function:
if __name__ == "__main__":
#...
sys.exit(someFunction(someParameter))
Which is the most correct option according to style?
Both are equivalent, but I think you know this. If you are asking from a style perspective, I'd say you should always favour the option that is more readable. In this case, I think the first one is more readable.
Python uses snake_case by convention so you might consider also using that to define your variables and functions.
You might also want to change the name of the result variable to be more descriptive.
exit_status = some_function(some_parameter)
sys.exit(exit_status)
Hopefully, this was what you were after.
Edit: #AnttiHaapala made a good point that succeeded isn't the best variable name either as 0 is Falsy in Python but considered a pass as an exit code. I've renamed it to exit_status.

Access value of function attribute 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 6 years ago.
Improve this question
Suppose I have a function -
def foo(x,y):
pass
a = foo(5,6)
How do I access the values 5 and 6 from a?
From the code you have shown us, you cannot -- 5 and 6 were passed in to foo, you didn't keep a copy of them, foo didn't keep a copy of them, so they are gone.
So, as the above paragraph hinted, somebody has to keep a copy of those arguments if you want to do something else with them later, and while it is possible to have a function do so, that's not really what they are intended for. So your easy options are:
make foo a class that saves the arguments it was called with (which is still highly unusual), or
save the arguments yourself (arg1, arg2 = 5, 6 for example)
You can't. You'd need to use an object.

Categories