Access Class Properties through Instance Name
Can I access class properties through an instance name?
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:
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
2017-09-09, 117👍, 0💬
Can You Specify the "new line" Character in Single-Quoted Strings? You can not specify the "new line...
How To Copy Array Values to a List of Variables in PHP? If you want copy all values of an array to a...
How To Control Vertical Alignment? By default, text in all table cells are aligned to the top vertic...
Where to find tutorials on how to use PHP built-in functions to manage arrays? A collection of tutor...
Where to find tutorials on how to Read and Write Files in PHP? A collection of tutorials to answer m...