Tools, FAQ, Tutorials:
'class' - Class Definition Statements
How to use the "class" statement to define a new function in Python?
✍: FYIcenter.com
You can use the "class" statement to define a new class in Python
with the following syntax:
def class_name(base_class): class properties assignment statement ... method definition statement ...
Here is a good example of "class" statement defining a class to present a user with the default base class "object":
>>> 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)) ... >>> joe = user() >>> joe.id, joe.name, joe.age (1, 'Joe', 25) >>> joe.dump() ID: 1 Name: Joe Age: 25 >>> >>> jay = user("Jay", 18) >>> jay.dump() ID: 2 Name: Jay Age: 18
2018-05-08, 1139👍, 0💬
Popular Posts:
What Is session_register() in PHP? session_register() is old function that registers global variable...
How to detect errors occurred in the json_decode() call? You can use the following two functions to ...
How To Read a File in Binary Mode in PHP? If you have a file that stores binary data, like an execut...
How To Merge Cells in a Column? If you want to merge multiple cells vertically in a row, you need to...
Where is API Management Service on my Azure Portal? If your IT department has signed up for an Azure...