Tools, FAQ, Tutorials:
What Is Class Method
What is class method in Python?
✍: FYIcenter.com
Class methods are functions defined inside the class definition statement block.
You can call to execute a class method in two formats:
1. Calling the method with the class name in the dot (.) expression format: class_name.method_name(...). For example,
>>> 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
... def dump(self):
... print("ID: "+str(self.id))
... print("Name: "+self.name)
... print("Age: "+str(self.age))
...
>>> jeo = user("Joe",30)
>>> user.dump(joe)
ID: 1
Name: Joe
Age: 30
2. Calling the method with the instance reference in the dot (.) expression format: instance_reference.method_name(...). In the case, the instance reference will be automatically provided as the first parameter to the function. For example,
>>> 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
... def dump(self):
... print("ID: "+str(self.id))
... print("Name: "+self.name)
... print("Age: "+str(self.age))
...
>>> jeo = user("Joe",30)
>>> joe.dump()
ID: 1
Name: Joe
Age: 30
The second format is easier to use, if the method is performing some operations on the instance of the class. But you have to code the class method to take the first parameter as the instance itself.
⇒ '__init__()' Class Method to Initialize New Instance
⇐ Create New Instances of a Class
2018-01-27, ∼2083🔥, 0💬
Popular Posts:
How to search for the first match of a regular expression using re.search()? The re.search() functio...
How To Loop through an Array without Using "foreach" in PHP? PHP offers the following functions to a...
Where to find tutorials on PHP language? I want to know how to learn PHP. Here is a large collection...
What's Wrong with "while ($c=fgetc($f)) {}" in PHP? If you are using "while ($c=fgetc($f)) {}" to lo...
How to use "xml-to-json" Azure API Policy Statement? The "xml-to-json" Policy Statement allows you t...