Answer the question
In order to leave comments, you need to log in
How to get the initial value of an attribute?
response is a.__class__().health
for example
class Some_class:
self.health = 15
a= Some_class()
a.health=10
Answer the question
In order to leave comments, you need to log in
print(Some_class.health)
When an attribute value is assigned to an object a, the connection with the initial value is lost and it is impossible to get it through the object a, but it is still possible through the Some_class class. Here, however, there is a nuance ...
P.sy when declaring a static variable in a class, you do not need to write self
Self is needed inside methods and is a reference to an object so that you can interact with it inside a function through this reference
For example, if health is dynamic and you want to get the value from the instance.
class SomeClass:
def __init__(self, health=20):
self._health = health
self._init_health = self._health
@property
def health(self):
return self._health
@health.setter
def health(self, value):
self._health = value
@property
def init_health(self):
return self._init_health
class SomeClass:
def __init__(self, health=20):
self._health = health
import inspect
inspect.getargspec(SomeClass.__init__)
self.health = 15
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question