change pointed instance inside a function in python - python

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)

Related

correct way to return variables as arguments to method

A noob confused question,
I have two methods in a class as :
from example import sample2
class sample1:
def m1():
a='apple'
b='ball'
return sample2.m3(a,b)
def m2():
a='ant'
b='bat'
c='cat'
return sample2.m3(a,b,c)
in example.py:
class sample2:
def m3("here I want to access any `a`,`b`,`c` of respective m1 and m2"):
.....
Iam sorry if this question makes no sense, but when I try to access only this as:
class sample1:
def m1():
a='apple'
b='ball'
return sample2.m3(a,b)
in example.py:
class sample2:
def m3(a,b):
print(a)
a has value apple, so similar way why can't I access any value of a,b,c from that particular m1,m2 returned ?
This is how you use decorators. More information how decorator works can be found in for example here: https://www.datacamp.com/community/tutorials/decorators-python
I would suggest you to first try to better understand concept of class and objects. Example tutorial: https://www.w3schools.com/python/python_classes.asp
This post could also help you to understand how staticmethod decorator works - What is the difference between #staticmethod and #classmethod?
from example import sample2
class sample1:
#staticmethod
def m1():
a='apple'
b='ball'
return sample2.m3(a,b)
#staticmethod
def m2():
a='ant'
b='bat'
c='cat'
return sample2.m3(a,b,c)
example.py file with explanation:
class sample2:
#staticmethod
def m3(a, b, c=None): # it works exactly the same as m3 function that is outside the class
print(a)
# this can be used without creating an object of sample2 class, example:
# sample2.m3(a="apple, b="ball")
def m3_method(self, a, b): # this one requires object on which it can be called
print(a)
# you have access to sample2 class object via self parameter, example of code:
# sample2_object = sample2() # you create object of sample2 class here
# sample2_object.m3_method(a="apple", b="ball") # you call m3_method on sample2_object here
def m3(a, b, c=None): # default value of c is add so you can either call it with 2 or 3 arguments
# example calls:
# m3("a", "b")
# m3("a", "b", "c")
print(a)
You should be able to run this code and I think it gives you an idea how Python classes can be used.
Variables in Python always apply to a specific scope, such as a class, function or closure. Python uses lexical scoping, which means scopes are only connected by nesting in the source code. Most importantly, variables in different scopes are not connected at all.
When you "pass a variable" to a function, you are actually passing only the value around. The variable does not exist in other functions (unless they are nested) nor the surrounding scope.
def nested(a):
a = 3
print('a =', a) # a = 3
def parent():
a = 4
nested(a)
print('a =', a) # a = 4
parent()
print(a) # NameError: name 'a' is not defined
Functions should primarily exchange data by calling with input and returning results:
def nested(a): # receive input
a = 3
print('a =', a) # a = 3
return a # return output
def parent():
a = 4
a = nested(a) # replace a with result of nested(a)
print('a =', a) # a = 3
parent()
Note that only values are passed in and returned. The above could would behave exactly the same if you renamed a in either function.
When working with class instances, the instance itself works as a namespace (similar to a scope). Methods of that instance can exchange data by modifying attributes of the instance. The instance is always passed as the first argument to methods:
class Example():
"""An example for setting attributes on an instance"""
def __init__(self):
self.a = 0
def nested(self):
self.a = 3
print('self.a =', self.a) # self.a = 3
def parent(self):
self.a = 4
print('self.a =', self.a) # self.a = 4
self._nested()
print('self.a =', self.a) # self.a = 3
instance = Example()
print(instance.a) # 0
instance.parent() # self.a = 4
# self.a = 3
To exchange data between objects, methods should also primarily exchange data by calling with input and returning results :
class Example():
"""An example for setting attributes on an instance"""
def __init__(self, a):
self.a = a
def multiply(self, value):
return self.a * value
instance = Example(6)
print(instance.multiply(10)) # 60

How to get object attributes to update dynamically in Python

I'd like to create a class that has 2 input attributes and 1 output attribute such that whenever one of the input attributes are modified the output attribute is modified automatically
I've tried defining the attributes as instance variables within and outside the constructor function but in either case, after instantiating the object, the output attribute remains fixed at the value set at the moment of instantiation
class Example():
def __init__(self,n):
self.name=n
inA=1
inB=1
if inA==1 and inB==1:
outA=1
else:
outA=0
when instantiated outA is set to 1 as expected
but if I try to update:
object.inA=0
object.outA remains 1 whereas I need it to be updated to 0
Trying to avoid the use of functions if possible. New to python and OOP so sorry if this question is nonsensical or has an obvious answer
If you want instance attributes that depend on other instance attributes, properties are the way to go.
class Example:
def __init__(self, n):
self.name = n
self.inA = 1
self.inB = 1
#property
def outA(self):
return self.inA and self.inB
You access outA like a regular instance attribute, obj.outA.
>>> my_obj = Example("example")
>>> my_obj.outA
1
Changing the attributes inA and inB affect outA.
>>> my_obj.inA = 0
>>> my_obj.outA
0
You can create a function in the class and some other minor changes:
class Example():
def __init__(self,n):
self.name=n
self.inA=1
self.inB=1
def f(self):
if self.inA==1 and self.inB==1:
self.outA=1
else:
self.outA=0
To call it:
a = Example('foo')
a.inA = 0
a.f()
print(a.outA)
Output:
0
As you can see, taking out:
a.f()
line would make it give an error:
AttributeError: 'Example' object has no attribute 'outA'
Do you want it to return your output?
Expanding on U9-Forward's answer:
class Example():
def __init__(self,n):
self.name = n
self.inA = 1
self.inB = 1
def f(self):
return self.inA and self.inB

Can we access class method variable using dot notation in python

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

variable defined inside __init__ not reading

What I have understood is to define variables inside init magic method. I did but the next method is not reading it.
Any help?
class Foo:
var = 9
def __init__(self, a, b):
self.i = a
self.j = b
def add(self, a, b):
print a+b
bar = Foo(5, 5) # create object
print bar.var # access class variable
o/p:
9
Why does it not print
10
9
If you want to run the code inside add, you must call it.
bar = Foo(5, 5) # create object
bar.add(5,5)
print bar.var # access class variable
You called the variables self.i and self.j, not a and b, so that's how you need to refer to them. add() should be defined like this:
def add(self):
print self.i+self.j

How do I pass variables between class instances or get the caller?

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()

Categories