Let's say we have a Pet class in Python:
class Pet(object):
num_pets = 0
def __init__(self, name):
self.name = name
Pet.num_pets += 1
def speak(self):
print("My name's %s and the number of pets is %d" % (self.name, self.num_pets))
I want the init method to create an instance, but also update an attribute on the class. Is there a more elegant way to do this than the code above? I tried passing in self
and cls
to the init method, and then referring to cls instead of Pet where num_pets is incremented, but that didn't work.
You could use a classmethod
to increment the number of pets:
class Pet(object):
num_pets = 0
def __init__(self, name):
self.name = name
self.incr_num_pets()
@classmethod
def incr_num_pets(cls):
cls.num_pets += 1
Alternatively, you could increment num_pets
on type(self)
:
def __init__(self, name):
self.name = name
type(self).num_pets += 1
though I find this second method to be slightly less elegant even if it is less typing.
In your case you could use __new__
together with __init__
:
class Pet(object):
num_pets = 0
def __new__(cls, *args, **kwargs):
cls.num_pets += 1
return object.__new__(cls)
def __init__(self, name):
self.name = name
You can access own class using self.__class__
, it means that your __init__
can looks like:
def __init__(self, name):
self.name = name
self.__class__.num_pets += 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With