I have the following code segment :
class A:
def __init__(self):
self.state = 'CHAT'
def method1(self):
self.state = 'SEND'
def printer(self):
print self.state
class B(A):
def method2(self):
self.method1()
print self.state
ob_B = B()
ob_A = A()
ob_B.method2()
ob_A.printer()
This gives me the output :
SEND
CHAT
I want it to print :
SEND
SEND
That is, when B.method2 is modifying self.state by calling self.method1, I want it to modify the already existing value of self.state = 'CHAT' in A's instance. How can I do this?
The instance is passed as the first argument to each of your methods, so self is the instance. You are setting instance attributes and not class variables.
class A:
def __init__(self):
A.state = 'CHAT'
def method1(self):
A.state = 'SEND'
def printer(self):
print A.state
class B(A):
def method2(self):
self.method1()
print B.state
ob_B = B()
ob_A = A()
ob_B.method2()
ob_A.printer()
SEND
SEND
ob_B = B()
ob_A = A()
ob_B.method2()
ob_A.printer()
You need to call ob_B.method2() -- without the parentheses that statement is just a reference to the function and doesn't actually call it.
You can call the printer() method by using object of B so that you will get the updated value.
ob_B = B()
ob_A = A()
ob_B.method2()
ob_B.printer()
Related
How to use variable outside of function which is define inside of function?
And Function should declare in class.
class A:
def aFunction(self):
aVariable = "Hello"
Now here I want to use that aVariable
If you want to use this variable within the class A, how about using an instance variable?
class A:
def aFunction(self):
self.aVariable = "Hello"
Now you can use self.aVariable in another function of the same class
There are definitely more options that maybe others will provide, but these are the options I have come up with.
Use return
class A:
def aFunction(self):
aVariable = "Hello"
return aVariable
obj = A()
var = obj.aFunction()
print(var)
use global
class A:
def aFunction(self):
global aVariable
aVariable = "Hello"
obj = A()
obj.aFunction()
print(aVariable)
You can use self to your advantage
class A:
def __init__(self):
self.aVariable = None
def aFunction(self):
self.aVariable = "Hello"
obj = A()
obj.aFunction()
print(obj.aVariable)
To use a variable from a class outside of the function or entire class:
class A:
def aFunction(self):
self.aVariable = 1
def anotherFunction(self):
self.aVariable += 1
a = A() # create instance of the class
a.aFunction() # run the method aFunction to create the variable
print(a.aVariable) # print the variable
a.anotherFunction() # change the variable with anotherFunction
print(a.aVariable) # print the new value
There are several methods you can try.
class A:
def aFunction(self):
self.aVariable = "Hello"
# you can access self.aVariable in the class
class A:
def aFunction(self):
aVariable = "Hello"
return aVariable
# use self.aFunction() whenever you need this variable
The return keyword will return the value provided. Here, you have provided self.aVariable. Then, you can assign the value to a variable outside the class and print the variable.
class A:
def aFunction(self):
self.aVariable = "Hello"
return self.aVariable
a = A() #==== Instantiate the class
f=a.aFunction() #==== Call the function.
print(f)
This will print: Hello
I wrote a class with methods that overwrites some methods of a parent class only when I want them to overwrite. Other times I call super of that method so that only things written in parent class method should execute. I observe that this works when I store data but not when I retrieve that data. A simplified take that shows the exact problem:
# parent class
class A(object):
def __init__(self):
self.var = {}
def assigner_method(self, value):
self.var = value
def returning_method(self):
return self.var
# child class
class B(A):
def returning_method(self):
#Do nothing
super(B, self).returning_method()
# What obviously works
class C(object):
def some_method(self):
self.obj = A()
self.obj.assigner_method("ABCD")
resp = self.obj.returning_method()
print resp
# What doesn't work:
class D(object):
def some_method(self):
self.obj2 = B()
self.obj2.assigner_method("ABCD")
resp = self.obj2.returning_method()
print resp
Now, this works:
print C().some_method()
ABCD
And this fails:
print D().some_method()
None
Putting some prints here and there, I see that setting the data self.var using self.obj2 works. Also when fetching data using self.obj2, the parent class returning_method prints returning data ABCD but when print at the caller, it says data received is NoneType. I think I did some fundamentally wrong here. Any help appreciated.
I have an instance variable from a class and I want to execute some code when there is a change in my variable.
I'm aware of the property and Observer pattern event handling but I don't think it helps in my case.
Example:
class Thing:
def __init__(self):
self.thing = []
self.thing2 = ""
def code_that_executes(self):
self.thing2 = self.thing[0]
s = Thing()
s.thing.append("Something") #The event
You can implement setattr on your class. Here is an example:
class A:
def __init__(self):
self.A = 5
def __setattr__(self, name, value):
if name == "A":
print("A has changed to: {0}".format(value))
Now when you have an object `foo = Foo()` and call `foo.bar = 5` you get the result:
bar changed to 5
See https://docs.python.org/3/reference/datamodel.html#object.setattr
Note:This will print during the init call as well.
Consider the following code:
class A:
def hi(self):
print 'A'
def change(self):
self = B()
class B(A):
def hi(self):
print 'B'
and
test = A()
test.hi() -> prints A
test.change()
test.hi() -> prints A, should print B
Is there some way to make this principle work, so changing the object reference 'test' from withing the class/object itself?
Objects have no concept of the variable that contains them - thus, you can't do exactly what you're trying to do.
What you could do is have a container that is aware of the thing it contains:
class Container(object):
def __init__(self):
self.thing = A()
def change(self):
self.thing = B()
def hi(self):
self.thing.hi()
test = Container()
test.hi() # prints A
test.change()
test.hi() # prints B
there is a way to pass a value or a variable from a class to another class without having to pass through the main function
I'm using python
well, of course you can access other objects attributes in methods of a specific object. e.g:
class A(object):
def method(self, other):
other.somevar = 5
class B(object):
pass
def main():
a = A()
b = B()
b.somevar = "Hello World"
a.method(b)
print(b.somevar) # now prints '5'