Calling a method from a constructor in python [duplicate] - python

This question already has answers here:
Python function calling order
(5 answers)
Closed 3 years ago.
The initial question I had was whether or not a method could be called from a Python constructor. The answer here suggests that it can, but this surprised me some as, based on my reading, python cannot use something that hasn't been previously defined in the code. Looking at the answers in the linked thread, it seems like they are using a method in the constructor that is defined later in the text. How is this possible in python?
Thanks.

When you would be running the code the class would be created , the function wouldn't be called
The function would only be called by object in main function by that time the definition of function would be compiled and known.
Try this
Declare a class and then call a function which is not a member of class * and also not declared
And declare it later , it would throw an error

Related

Python variable inheritance not working (as expected) [duplicate]

This question already has answers here:
How to create dynamical scoped variables in Python?
(4 answers)
Closed 2 years ago.
Why, in this code, can't I access db or file from inside do_more()?
def do_stuff(queue):
db = getDbConn()
while True:
file = queue.get() #DirEntry object
...
do_more(otherObject)
q.task_done()
I'm aware I could pass db and file as arguments to do_more(), but I feel like those variables should just be available in functions called from other functions. It works that way from main to first level functions. What am I missing here?
In the code you provided, you don't even attempt using the variables from do_stuff.
But as a general rule, you should be able to use variables from a parent function inside a child function, either by passing them as variables or by using them when initializing the child function, like this:
def foo():
foo2 = "foo"
def bar():
print(foo2)
bar()
foo()
If i did not answer your question let me know.
no , you cant access those variables , I know what you think , which is wrong.
you can access variables inside loops and if statements, not here.
imagine you have a function which is used in many different places, in that case you have access from this function to many variables which makes things complicated.
functions should be stand-alone objects which take some arguments do stuff and return something.so inside a function scope you can only see variables which are defined there and the arguments passed from the parent function using parenthesis.

Checking argument values at runtime in python [duplicate]

This question already has answers here:
Obtaining closures at runtime [duplicate]
(1 answer)
How to open a closure in python?
(5 answers)
Closed 2 years ago.
I would like to know if there is any method to check whether two functions have the same arguments at runtime in python 3.
Basically, I have this function (func) that takes two arguments and perform some sort of computation. I want to check whether or not a and b have the same arguments' values at runtime
a = func(2, 3)
b = func(2, 3)
a.argsvalue == b.argsvalue
It is not feasible to run the code before and check the results because I am implementing a lazy framework. My main goal is to be able to understand what are the arguments of the function because there is one variable argument that I do not care but there is one static that is created before running the function.
##EDIT
I actually solved this problem using the inspect module (getclosure) for those who are interested. Thank you so much for the comments it helped me to familiarize myself with the terminology. I was actually looking for the closure, which I assigned dynamically.
when you do this - a.argsvalue == b.argsvalue you try to access a member of the value returned from the function.
so, if your "func" would return an object having the args you called it with (which sound like a weird thing to do) you would be able to access it.
anyway, if you need these values, just store them before sending them to the function, and then you can do whatever you want with them.

How to assign a variable inside a function with the variable as an input for the function? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 3 years ago.
def convert(target):
#Some code here
target = 20
convert(x)
print(x)
I expected the output to be 20, but there is always an error. "NameError: Name x is not defined" Is there some way that I could fix that?
It sounds like you are trying to pass the value by reference, which goes against the grain of Python, which passes variable by assignment. The easiest "solution" is to refactor your code so it returns the new value, as others have suggested.
In some languages (e.g. PHP), you can specify that certain arguments are to be passed by reference (e.g. by prefixing the variable name with a "&"), but in practice this often makes the code harder to read and debug.
For more information, see:
How do I pass a variable by reference?

Can a dictionary be used to pass variables to a function? [duplicate]

This question already has answers here:
Passing a dictionary to a function as keyword parameters
(4 answers)
Closed 5 years ago.
I have a class with an init function that needs ~20 variables passed to it. Instantiations of the class are rather ugly:
some_var = MyClass(param1=mydict('param1')
param2=mydict('param2')
param3=mydict('param3')
...
param20=mydict('param20')
I keep all of those parameters in a dictionary for easy handling already. Not all parameters are necessary so I don't want the class to accept only a dictionary object I don't think.
I'm wondering if I there's a nice trick to clean this up by specifying that the parameters to init are to be taken from the dictionary?
I'm just looking for tips and tricks on tidying this up a little. I have quite a few long/ugly instantiations of this class.
You can use keyword argument expansion:
some_var = MyClass(**mydict)

How can I forward-declare/prototype a function in Python? [duplicate]

This question already has answers here:
Does Python have class prototypes (or forward declarations)?
(6 answers)
Closed 8 years ago.
How do I prototype a method in a generic Python program similar to C++?
# Prototype
# Do Python prototyping
writeHello() # Gives an error as it was not defined yet
def writeHello():
print "Hello"
Python does not have prototyping because you do not need it.
Python looks up globals at runtime; this means that when you use writeHello the object is looked up there and then. The object does not need to exist at compile time, but does need to exist at runtime.
In C++ you need to prototype to allow two functions to depend on one another; the compiler then can work out that you are using the second, later-defined function. But because Python looks up the second function at runtime instead, no such forward definition is needed.
To illustrate with an example:
def foo(arg):
if not arg:
return bar()
def bar(arg=None):
if arg is not None:
return foo(arg)
Here, both foo and bar are looked up as globals when the functions are called, and you do not need a forward declaration of bar() for Python to compile foo() successfully.

Categories