What is the benefit to reference a method in a class? [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 4 years ago.
Improve this question
I found code on GitHub that is similar to my ExampleClass, but I don't understand the benefit of using getx = calcualte_x. I feel like it is somewhat close to #property, but it is not obviously.
what is the benefit of that line and why not just use class-instance.calcualte_x(add) instead of class-instance.getx(add)
class ExampleClass():
def __init__(self, x_val):
self.x = x_val
def calcualte_x(self, add):
x = self.x + add
getx = calcualte_x

It can be an alias - for ease of use or for backward compatibility for example.

Related

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)

In Dropout(0.8)(x), what is (x) meant for? [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 4 years ago.
Improve this question
In the following Python code, what does the (x) mean?
Dropout(0.8)(x)
(x) uses the preceding function reference (returned by Dropout(0.8)) to make a function call with x as the argument.
For example:
def Dropout(a):
def func(b):
return a + b
return func
x = 0.2
print(Dropout(0.8)(x))
would output 1.0.

Why python does not update an external parameter with assignment? [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 7 years ago.
Improve this question
Can anyone explain the reason why python behaves differently in the following two cases please? Much appreciation.
def modifyNone(x):
print("B4:"+str(x))
# x.append(5)
x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[]
Method:
def modifyNone(x):
print("B4:"+str(x))
x.append(5)
# x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[5]
Python is pass by value, so you'll have to reassign a returned value like so:
a = modifyNone(a) # where the function returns a value
You don't seem to understand variable scope as well, try the documentation.

Is "retval" good python style? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a strong reason to use a retval variable vs just returning a calculation?
def add(self, x, y):
return x + y
versus
def add(self, x, y):
retval = x + y
return retval
I feel like I usually see retval (or some other named variable) in code examples but it seems like a (small) waste to me.
In this example it won't make a difference but in longer functions it can be beneficial (for subjective reasons) to have one result or retval variable and only return that value at the end. This can make the code easier to understand (provided it is structured well) by only having one return location.
That being said, it depends on the developer's preferences and in some functions multiple return locations are equally readable.
The only reason to use it is when you want to use the value before you return it. for example, printing it before you return.

Importing from a function within a function 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 9 years ago.
Improve this question
I have a file (test.py):
def main():
def hello():
x = 10
Then I have another file (test2.py):
from test import *
print(main.hello.x)
Now I know that print(main.hello.x) won't work but I want something like that.
I want to access a variable inside another file that is in a nested function.
How can I do that?
This feels quite unpythonic, but this will work:
def main():
def hello():
hello.x = 10
return hello
main.hello = hello
return main
print(main.hello.x) #returns 10
a more pythonic approach is to use a class.

Categories