How to change variable of main class from inherited class? - python

I need to change object variable directly from inherited class.
Here is my code example:
class A(object):
def __init__(self,initVal=0):
self.myVal = initVal
def worker(self):
self.incrementor = B()
self.incrementor.incMyVal(5)
class B(A):
def incMyVal(self,incVal):
super().myVal += incVal
obj = A(5)
print(obj.myVal)
obj.worker()
print(obj.myVal)
But it doesn't work:
AttributeError: 'super' object has no attribute 'myVal'
I also tried to use global/nonlocal keywords with my variable in B class, but no luck.
In my main case, the B class is an event handler. And it should change the attribute of an object when an event fires. So I'm not able to use return in the incMyVal method.

super() can only search for class attributes in the class MRO, not instance attributes. myVal is set on an instance of the class, not on a class itself.
There is only ever one instance; it doesn't matter if code from class A or a derived class is altering attributes on an instance, it is just one namespace.
However, in your case, you shouldn't even be using inheritance. You are trying to use an independent, second instance to alter the attributes of an instance of A. Class inheritance doesn't give you access to instances of the base class like this.
Refactor B to take an instance of A, then act on that instance:
class B:
def __init__(self, ainstance):
self.ainstance = ainstance
def incMyVal(self, incVal):
self.ainstance.myVal += incVal
Note that B is not a subclass of A here; it is not a (specialised) object of the same type at all; it is a different kind of thing, something that increments attributes of another object.
Pass in the instance when you create an instance of B:
def worker(self):
self.incrementor = B(self)
self.incrementor.incMyVal(5)
This does create a circular reference, which can keep objects alive for longer than perhaps needed. You may want to use a weak reference instead:
import weakref
class B:
def __init__(self, ainstance):
self.ainstance_ref = weakref.ref(ainstance)
def incMyVal(self, incVal):
ainstance = self.ainstance_ref()
if ainstance is not None:
ainstance.myVal += incVal
Now B instances only hold a weak reference to their A instance, and will do nothing if that instance no longer exists.

Related

We can create instance attrbitues for objects in Python. Can we create instance methods (not class Methods) as well in Python?

How To create a methods which are common to a particular object just like creating instance attrbitue obj.instance_attribute
A method which belongs specifically for a single object ?
The link contains the code. I need to create method only for this object and not all instance of class.
Creating class methods and attribute. The instance attrbitue. How to create instance methods
class A():
def init(self):
self.class_variable = 999999
def class_methods(self):
#available to all object
print("Hey")
obj = A()
obj.class_variable
999999
obj.class_methods()
Hey
obj.instance_attribute = 40404040 #common to particular object
obj.instance_attribute
40404040
#a method which is common to only this object
obj.new_method():
SyntaxError: invalid syntax
obj.new_mehtod(self):
SyntaxError: invalid syntax
I think you are mixing up terminology. Every "normal" method is a instance method - that means it applies the function without affecting any other instances of this class. To reference the instance, use the passed self keyword.
Defining a method for a single instance inside the generator/ class definition does not make sense in an OOP-context. If you create a car class, every instance of this class should be able to access its methods, like drive().
The only way to add a unique function is to add it after instantiating the object. This can be done with the types.MethodType method, which binds the function to the class instance:
from types import MethodType
def fly(self):
print(f"i, {self.name}, can fly")
class Car:
def __init__(self, name):
self.name = name
car_1 = Car("car one")
car_2 = Car("car two")
car_1.fly = MethodType(fly, car_1)
car_1.fly() # i, car one, can fly
car_2.fly() # AttributeError: 'Car' object has no attribute 'fly'
As you can see, car_1 has the class fly, which references car_1's name, while car_2 does not have this function.
But you should seriously reconsider what you are trying to achieve here.

what does __init__ of one class do in another class body?

class Borg:
"""Borg pattern making the class attributes global"""
_shared_data = {} # Attribute dictionary
def __init__(self):
self.__dict__ = self._shared_data # Make it an attribute dictionary
class Singleton(Borg): #Inherits from the Borg class
"""This class now shares all its attributes among its various instances"""
# This essentially makes the singleton objects an object-oriented global variable
def __init__(self, **kwargs):
Borg.__init__(self) # WHAT DOES THIS DO? Why is borg initialized with the self of this class?
self._shared_data.update(kwargs) # Update the attribute dictionary by inserting a new key-value pair
def __str__(self):
return str(self._shared_data) # Returns the attribute dictionary for printing
Borg.__init__(self) What does this do? What is borg initialized with the self of this class?
When you create an object of a derived class, Python only calls the __init__ special method for the most derived class. If ancestor classes require initialization, it is up to the programmer of the derived class to explicitely call __init__ on those classes.
What you show is the old Python 2 compatible idiom: Borg.__init__ is the unbound initialization method of the Borg class. If you call it you have to pass it the object as the first parameter, because in Python if a is an object from a A class having a m method with no parameters, a.m() is the same as A.m(a) - if there are parameters, they will just come after the object.
In this case and assuming Python 3, the most idiomatic way would be:
def __init__(self, **kwargs):
super().__init__() # exactly the same as Borg.__init__(self)
...
Only the "child init" will be executet.
So in your case if you call Singleton, only the Singleton init will be executet.
creating an instance of the Singleton class like this mySingleton = Singleton(someArgs) will call the constructor def __init__(self, **kwargs):initializing the new instance of your class. the first thing that is being initialized by the initializeation code block is to call the initialization of the underlying class Borg by calling Borg.__init__(self), this happens in pretty much all languages granted sometimes in a slightly different layout, but it has to happen in order to pass in any required arguments to the underlying base class

Can python classmethod change value of an instance variable and vice versa?

Can python classmethod change value of an instance variable?
And can normal methods change value of the class variable?
Any function in python can change any variable it has access to.
But that's a broad answer. Let's examine both of your questions in turn:
Can a class method change the value of an instance variable?
Consider the following class:
class A:
def __init__(self):
self.prop1 = 2
self.prop2 = "Beaver"
#classmethod
def do_something(cls):
...
If we're inside do_something, we don't know whether any specific instances exist. If you were to pass an instance of A into the method via an argument, then we could do whatever we wanted to that instance, but unless the instances of A are stored somewhere we can see, you can't modify them from within.
One workaround to this is keeping a list of instances in a class variable:
class A2:
instances = []
def __init__(self):
# setting instance variables
self.prop1 = 2
self.prop2 = "Beaver"
# setting class variables
A2.instances.append(self)
#classmethod
def do_something(cls):
...
This time, we would be able to access an instance from within do_something, because that instance would be somewhere in instances, which is a class variable.
Can instance methods change the value of class variables?
Yes, because any instance method is necessarily going to have access to the class of the instance. You can write this explicitly, as we did above in class A2, or if you were dealing with polymorphism you could access the __class__ attribute of the instance:
class B(A2):
def __init__():
self.prop1 = 7
self.prop2 = "Chihuaua"
self.__class__.instances.append(self)
The principle "you can change anything you can see" is fun and dangerous to mess around with. For example, you can redefine built-in methods, and you can even change class methods from outside of them (here, I will redefine the __init__() method of the class B outside of the class itself - now all new instances of B will use my constructor instead of the one B initially came with):
def my_new_init(self):
self.prop1 = 10
self.prop2 = "bear"
B.__init__ = my_new_init
It's extremely dangerous to go down this rabbit hole unless you know exactly what you're doing.
Can python classmethod change value of an instance variable?
No. Class object does not have direct access to any instance variable.
can normal methods change value of the class variable?
Yes; but the change will be on it's own view (unless the name is referred by using class explicitly e.g. by name or self.__class__) i.e. it won't change the class variable as seen by the class or any other object. If the class object is referred explictly the class variable will be changed as you can imagine.
No and Yes.
A classmethod has no access to the instance object.
While an instance method can access the class object via self.__class__.
It's however a bad idea to change a value that's global for a class in an instance method because that's an unexpected side effect.
Class-level attributes are transparently accessible via self just like instance-specific ones. So if you need to override a class-level attribute for a specific instance, just assign it in self:
In [1]: class C(object):
...: ca=1
...: cb=1
...: def inc_ca(self):
...: self.ca+=1
...:
In [2]: c=C()
In [4]: c.ca
Out[4]: 1
In [5]: c.ca=2
In [6]: c.ca
Out[6]: 2
In [7]: c.inc_ca()
In [8]: c.ca
Out[8]: 3
In [9]: C.ca
Out[9]: 1

Python official name for an attribute that is not a method

According to my understanding the data members of objects in Python are referred to as 'attributes'.
Attributes that are callable are referred to as an object's 'methods', but I couldn't find a name for non-callable attributes, such as val in the following example:
class C:
def __init__(self):
self.val = 42. # How would this be called?
def self.action():
"""A method."""
print(self.val)
I am sure different people may call val different things like 'field' or 'variable' but I am interested in an official name.
Surprisingly hard to find official information on this topic. After reading this article I do believe it should simply be called Class Variable and Instance Variable.
Attributes, Properties, Methods and Variables
Attribute is the collection name for the three names Property, Method and Variable. The latter two are prefixed by either Class or Instance. A property can only belong to the Class.
class Foo:
a = 1
def __init__(self):
self.b = 2
#property
def c(self):
return 3
#classmethod
def d(cls):
return 4
def e(self):
return 5
Foo.a # Class Attribute: Class Variable
Foo().a # Class Attribute: Class Variable
Foo().b # Instance Attribute: Instance Variable
Foo.c # Class Attribute: Property
Foo.d # Class Attribute: Class Method
Foo().d # Class Attribute: Class Method
Foo.e # Class Attribute: Class Method
Foo().e # Instance Attribute: Instance Method
Sources
Difference between Class and Instance methods
How do I assign a property to an instance in Python?
What's the difference between a Python "property" and "attribute"?
https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces
Diagram made in Creately
I'm not sure if one exists, but I'd suggest just "instance attribute".
Features about this naming:
It excludes methods. Methods are all callable class attributes, so this wording excludes all methods.
It includes callable instance attributes. Consider the following code:
class Container:
def __init__(self, item):
self.item = item
c = Container(x)
c.item # is an "instance attribute"
c.item == x # True
Note that c.item is an "instance attribute" regardless of whether or not it's callable. I think this is behaviour you're after, but I'm not sure.
It excludes non-callable class attributes, e.g.
class SomeClass:
x = 5 # Is not an "instance attribute"
It includes per-instance attributes, e.g.
obj.x = 5
obj.x # Is an "instance attribute"
In the end, all of these features may be positives or negatives depending on specifically what you want. But I don't know specifically what you want, and this is as close as I can get. If you can provide more information, I can give a better suggestion.

Python Class Inheritance, __init__ and cls

The desired output of the code is that I have a class variable Team.stuff which has one entry holding the b instance, and the Player.stuff variable should be empty. Instead I get an error...
class Player:
stuff=[]
def __init__(self):
cls.stuff.append(self)
class Team(Player):
def __init__(self):
super(Team, self).__init__()
b=Team()
ERROR
cls.stuff.append(self)
NameError: global name 'cls' is not defined
I could pass the cls variable in the Team.__init__(), but I'm not sure if that is the "correct" way, and more importantly the Player.__init__() would need a class variable, and I'm not sure on the syntax on how to do that.
class Player(object):
stuff=[]
def __init__(self):
self.stuff.append(self)
class Team(Player):
def __init__(self):
super(Team, self).__init__()
b = Team()
print(Team.stuff)
prints (something like)
[<__main__.Team object at 0xb7519dec>]
Remember that cls is not a keyword in Python. Rather it is a convention for the first argument of a method when that method is supposed to be a class function and not one that is called on a particular instance of something.
If you want stuff to be an attribute of a function, you have to define it like so: self.stuff=[].
And then, when you refer to it in methods, do it with the self keyword as well: self.stuff.append(..).
The reason for this is that cls in your __init__ function is not defined. The argument names self and cls are just naming conventions. In reality, you could name them foo and bar for an instance and a class method respectively, and foo would point at the class instance whilst bar would point a the class itself.
Moreover, self.stuff would be valid as when searching for an attribute, if an object does not have an attribute of such name in it's __dict__ dictionary, the __dict__ of it's class is looked instead. If both are missing, further lookups are done according to mro order.
Notice that, once you set an attribute stuff on an object instance, it will shadow the class definition.
Explained by example:
class MyClassA(object):
stuff = []
class MyClassB(MyClassA):
def __init__(foobar):
# `foobar` points at the instance of `MyClassB` class.
# MyClassA.stuff gets `1` object appended to it.
foobar.stuff.append(1)
# This should print '[1]'
print foobar.stuff
# `MyClassB` object gets a _new_ attribute named `stuff` pointing at a list.
foobar.stuff = []
# This should print '[]'.
print foobar.stuff

Categories

Resources