Tools, FAQ, Tutorials:
Access Class Properties through Instance Name
Can I access class properties through an instance name?
✍: FYIcenter.com
Yes, you can access class properties through an instance name, as long as there is no instance property
that has the same name as a class property.
When you reference a property through the instance name in the following form:
instance_name.property_name
Python will search for the given property name in instance_name.__dict__ first. If match found, return the value. If not match found, continue with the next step.
In the next step, Python will search the given property name in class_name.__dict__. If match found, return the value. If not match found, return error.
The following Python code shows you a good example of accessing class property through a instance name:
>>> class user(): ... nextID = 1 ... def __init__(self,name="Joe",age=25): ... self.id = user.nextID ... self.name = name ... self.age = age ... user.nextID = user.nextID + 1 ... >>> x = user() >>> x.nextID 2
⇒ Defining and Using Python Code Modules
⇐ Where Are Instance Properties Stored
2022-09-24, ∼2089🔥, 0💬
Popular Posts:
How to Build my "sleep" Docker image from the Alpine image? I want the container to sleep for 10 hou...
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...
Can You Add Values to an Array without Keys in PHP? Can You Add Values to an Array with a Key? The a...
What is the Azure AD v1.0 OpenID Metadata Document? Azure AD v1.0 OpenID Metadata Document is an onl...
How To Remove Slashes on Submitted Input Values in PHP? By default, when input values are submitted ...