Below is the code
import os
class ABC:
def test(self,name):
var = 5
var2 = 10
dic = {"Ada": "adada"}
print "asdfdsadv"
var1 = "Ada"
var2 = "asada"
obj = ABC()
print obj.test("Ada").var1
I am looking for something like this. Can I achieve this in python
I know this is var variable in local to class. Is there someway by using global or something else to acheive this
Accessing a variable from a class method is not possible, you have to set the variable at the class level like this:
import os
class ABC:
def test(self,name):
var = 5
var2 = 10
dic = {"Ada": "adada"}
print "asdfdsadv"
self.var1 = "Ada"
var2 = "asada"
obj = ABC()
obj.test('Ada')
print obj.var1
You could chain obj.test('Ada').var1 in the same line by returning self into your test method.
I think this would work. The init(self) behave like a constructor in other languages. So in effect I am constructing a class in a class, to make your last line work properly. But like other suggested that is not the way Python is supposed to be used.
import os
class ABC(object):
def __init__(self):
pass
class test(object):
def __init__(self,name):
self.var = 5
self.var2 = 10
self.dic = {"Ada": "adada"}
print ("asdfdsadv")
self.var1 = "Ada"
self.var2 = "asada"
if __name__ == "__main__":
obj = ABC()
print (obj.test("Ada").var1)
What you are looking for are the class variables, usually defined as self.variable. Here an example of your code:
import os
class ABC:
def __init__(self):
self.var = 5
self.var2 = 10
self.dic = {"Ada": "adada"}
self.var1 = "Ada"
def test(self,name):
print self.var
print self.var2
print self.var1 + " " + name
obj = ABC()
print obj.dic # {'Ada': 'adada'}
print obj.dic["Ada"] # adada
obj.test("wow") # 5, 10, Ada wow
obj.var1 = "Ede"
obj.test("wow") # 5, 10, Ede wow
but as suggested in other answers, you may want to take a step back and check what is the scope of python variables
Forget about classes and consider functions in general.
When you define a function, any variables within its scope are local and only accessible from within the execution of that function. Once execution has finished, that's it, they are gone. This is fundamental; the only way of getting data from a function is by returning it.
Although it is a hack, you could return locals() (a dictionary of local variables), but this is terrible practice.
import os
class ABC:
def test(self,name):
var = 5
var2 = 10
dic = {"Ada": "adada"}
print "asdfdsadv"
var1 = "Ada"
var2 = "asada"
return locals()
obj = ABC()
print obj.test("Ada")["var1"]
If you return the object itself from the function and the variables are not local but instance variables it works.
class ABC:
def test(self, name):
self.var1 = "My name is {}".format(name)
return self
obj = ABC()
print obj.test('Ada').var1
Related
When you are debugging complex code, you sometimes need to transform:
def myfunction(self):
...
self.foo.bar = self.baz.bla
into
def myfunction(self):
...
self.foo.bar = self.baz.bla
print("myfunction", "self.foo.bar", self.foo.bar) # add this for debugging purposes
Is there a way (with a decorator or context manager or anything else) to automatically print the variable name and the value of the next line of code's assignement (and maybe also the current function)?
Example:
def myfunction(self):
...
with debug:
self.foo.bar = self.baz.bla
would output:
"myfunction self.foo.bar 123"
You can use the inspect module:
from inspect import currentframe
def f():
a = 5
debug_print("a")
def debug_print(var):
locals = currentframe().f_back.f_locals
print(f"{var} = {locals[var]}")
f()
See also here: Access parent namespace in python
I admit, it's only part of what you asked, but maybe a good start.
Edit: Ok what about this:
from inspect import currentframe, getsourcelines
class A:
def f(self):
self.b = 5
debug_print()
self.a = A()
self.a.a = 4
debug_print()
#staticmethod
def g():
A.c = 5
debug_print()
def debug_print():
frame = currentframe().f_back
locals = frame.f_locals
globals = frame.f_globals
source, start = getsourcelines(currentframe().f_back.f_code)
var_name = source[frame.f_lineno - 1 - start].split("=")[0]
tokens = var_name.strip().split(".")
var = locals.get(tokens[0], globals.get(tokens[0], None))
for t in tokens[1:]:
var = getattr(var, t)
print(f"{var_name} = {var}")
a = A()
a.f()
a.g()
At least now, it works with member attributes (including self), even nested. Also assignment of global variables, such as static attributes to the class.
I have three functions that want to change a global variable and can't combine into one function, the way I know right now is keep define "global" keyword in each functions to be able to access global variable. Is there a better way to do the same thing with out keep redefining the global variable? for example:
def fn1(self):
global NUM
NUM = 1
print "fn1 = ", NUM
def fn2(self):
global NUM
NUM = 2
print "fn2 = ", NUM
def fn3(self):
global NUM
NUM = 3
print "fn3 = ", NUM
NUM = 0
fn1(NUM)
fn2(NUM)
fn3(NUM)
Thank you
Why don't you define another function, which changes the value of the global variable with the argument provided as parameter. And call this in rest of your function. For example :
var=None
class A:
def change(self,num):
global var
var = num
def first(self,num):
self.change(num)
def second(self,num):
self.change(num)
def third(self,num):
self.change(num)
a=A()
a.first(1)
print 'value of global variable',var
a.second(2)
print 'value of global variable',var
a.third(3)
print 'value of global variable',var
Otherwise, if the scope of your global variable is supposed to be confined within your class then declare it as a member of the class, and let the functions change it's value. For example :
class A:
def __init__(self):
self.var=None
print "default = ", self.var
def first(self,num):
self.var=num
print "fn1 = ", self.var
def second(self,num):
self.var=num
print "fn2 = ", self.var
def third(self,num):
self.var=num
print "fn3 = ", self.var
a=A()
a.first(1)
print 'value of variable',a.var
a.second(2)
print 'value of variable',a.var
a.third(3)
print 'value of variable',a.va
You can make the global variable a mutable class and then mutate in place.
global_dict = {"data": 1}
global_list = [1]
class MutableInt:
def __init__(self, value=1):
self.value = value
global_int = MutableInt()
This works, but personally I wouldn't call that any better. With the global it is at least 100% clear that you change a global variable. Best would be to change your architecture to not need any global variables.
it seems like the function are part of a class , based on the self keyword , if so they all can access class variable without the need of global keyword, if they are not part of a class you can:
define each function with a given parameter to the function and make that function return a value.
def func1(param_1):
#work on param_1
return param_1
i'm newer in python but i have some package from other languages . Here's my question :
i need to change an instance reference inside a function.This instance is passed as parameter.
but i didn't know how to do it. I think i miss something in Python basics.The code bellow is given as example for what i want:
class Foo(object):
def __init__(self,a):
self.a = a
def func(a):
b = Foo(3)
a = b
var1 = Foo(5)
print(var1.a) # 5
func(var1)
print(var1.a) # it display 5 not 3
You can make func return a and then assign that to var1 as follows:
def func(a):
b = Foo(3)
a = b
return a
var1 = Foo(5)
print(var1.a) # 5
var1 = func(var1)
print(var1.a) # 3
>>> var1.a
3
What you were doing in your code is that you were changing the pointer for the local variable a in your func(a) method. However, if you want to change the var1 pointer, you have to assign the changed a variable that you passed as an argument.
One way is to use an umutable object such as the list and the property functions that automagically does what you need.
class Foo(object):
def __init__(self, value):
self._a = [value]
def __geta(self):
return self._a[0]
def __seta(self, obj):
self._a.insert(0, obj)
self._a.pop(1)
a = property(__geta, __seta)
var1 = Foo(5)
print var1.a
var1.a = 3
print(var1.a)
I am looking for a way to access a subclasses variables from the parent class which is instantiated in a different file. For example
basefile.py:
class A(object): #gets subclassed
var = 0 #place holder
def printTheVar(self):
print self.var
class B(object):
def buildAndCallA(self):
a = A()
a.printTheVar()
implementationfile.py:
from basefile import *
class A(A):
var = 10
if __name__ == '__main__':
b = B()
b.buildAndCallA()
When I run:
$ python implementationfile.py
I get 0. I want to get 10
When both parent class and implementation class are in the same file, this is obviously not a problem but I have a project structure which requires they not be:
somedir/
| basefile.py
| implementations/
| -- implementationA.py
| -- implementationB.py
| -- implementationC.py
I think that the abc module might help but my experiments with that have proven fruitless so far.
I'd suggest, if possible, you pass the class you want to use to the buildAndCallA method. So it should look something like this:
def buildAndCallA(self,cls):
a = cls()
a.printTheVar()
And then you can call it like this:
b.buildAndCallA(A)
Then it will use whatever version of the A class is in scope at the time it is called.
You could even set it up with a default parameter, so it will use the version of A in the base file by default, but you can still override it when necessary.
def buildAndCallA(self,cls=A):
a = cls()
a.printTheVar()
Then if you call b.buildAndCallA() with no parameter, it will construct an instance of the A class from the base file.
#James's answer got me most of the ways there. Here is a more global way to do it using three files for clarity (which is really how the project is organized anyways)
script.py:
if __name__ == '__main__':
if sys.argv[0] == 'useImplementation1'
import implementations.implementation1 as implementation
elif sys.argv[1] == 'useImplementation2':
import implementations.implementation2 as implementation
b = implementation.B(cls=implementation)
b.buildAndCallA()
basefile.py (notice the A = cls.A this is the key):
class A(object):
var = 0 #place holder
def printTheVar(self):
print self.var
class B(object):
def __init__(self,cls):
global A
A = cls.A
def buildAndCallA(self):
a = A()
a.printTheVar()
implementation1.py:
from basefile import *
class A(A):
var = 10
class foo():
def __init__(self)
self.var1 = 1
class bar():
def __init__(self):
print "foo var1"
f = foo()
b = bar()
In foo, I am doing something that produces "var1" being set to 1
In bar, I would like to access the contents of var1
How can I access var1 in the class instance f of foo from within the instance b of bar
Basically these classes are different wxframes. So for example in one window the user may be putting in input data, in the second window, it uses that input data to produce an output. In C++, I would have a pointer to the caller but I dont know how to access the caller in python.
As a general way for different pages in wxPython to access and edit the same information consider creating an instance of info class in your MainFrame (or whatever you've called it) class and then passing that instance onto any other pages it creates. For example:
class info():
def __init__(self):
self.info1 = 1
self.info2 = 'time'
print 'initialised'
class MainFrame():
def __init__(self):
a=info()
print a.info1
b=page1(a)
c=page2(a)
print a.info1
class page1():
def __init__(self, information):
self.info=information
self.info.info1=3
class page2():
def __init__(self, information):
self.info=information
print self.info.info1
t=MainFrame()
Output is:
initialised
1
3
3
info is only initialised once proving there is only one instance but page1 has changed the info1 varible to 3 and page2 has registered that change.
No one has provided a code example showing a way to do this without changing the init arguments. You could simply use a variable in the outer scope that defines the two classes. This won't work if one class is defined in a separate source file from the other however.
var1 = None
class foo():
def __init__(self)
self.var1 = var1 = 1
class bar():
def __init__(self):
print var1
f = foo()
b = bar()
Same as in any language.
class Foo(object):
def __init__(self):
self.x = 42
class Bar(object):
def __init__(self, foo):
print foo.x
a = Foo()
b = Bar(a)
Alternatively you could have a common base class from which both derived classes inherit the class variable var1. This way all instances of derived classes can have access to the variable.
Something like:
class foo():
def __init__(self)
self.var1 = 1
class bar():
def __init__(self, foo):
print foo.var1
f = foo()
b = bar(foo)
You should be able to pass around objects in Python just like you pass around pointers in c++.
Perhaps this was added to the language since this question was asked...
The global keyword will help.
x = 5
class Foo():
def foo_func(self):
global x # try commenting this out. that would mean foo_func()
# is creating its own x variable and assigning it a
# value of 3 instead of changing the value of global x
x = 3
class Bar():
def bar_func(self):
print(x)
def run():
bar = Bar() # create instance of Bar and call its
bar.bar_func() # function that will print the current value of x
foo = Foo() # init Foo class and call its function
foo.foo_func() # which will add 3 to the global x variable
bar.bar_func() # call Bar's function again confirming the global
# x variable was changed
if __name__ == '__main__':
run()