The specific method of python instantiation object

Python also uses the keyword class to create a class. The first letter of the class name is capitalized, with or without parentheses; the keyword new is not required to instantiate a class in Python (and there is no such keyword), and the instance of the class The similar function call method;

# coding:utf-8
# Create a class, the first letter of the class name is capitalized,Can be with or without parentheses

classStudent():
student_count=0def__init__(self,name,salary):
self.name=name
self.age=salary
Student.student_count+=1defdisplay_count(self):print('Totalstudent{}'.format(Student.student_count))defdisplay_student(self):print('Name:{},age:{}'.format(self.name,self.age))defget_class(self):
ifself.age =7andself.age<8:
return1
ifself.age =8andself.age<9:
return2
ifself.age =9andself.age<10:
return3
ifself.age =10andself.age<11:
return4
else:
return0

Create object of class (instantiate class)

There is no need to use the keyword new to instantiate a class in python (there is no such keyword), and the instantiation of a class is similar to a function call method.

student1=Student('cuiyongyuan',10)
student2=Student('yuanli',10)

student1.display_student()
student2.display_student()

student1_class=student1.get_class()
student2_class=student2.get_class()

Example extension:

The instantiation process:

classluffy_stu:
 def __init__(self,name,age,sex):
 self.name = name
 self.age = age
 self.sex = sex
 def eat(self):
 pass
if __name__=="__main__":
 stu1 =luffy_stu('bao',21,'male')
 # Instantiation process:
 #1. Is to first generate a stu1 object,
 #2. luffy_stu.__init__('stu1','bao',21,'male')Then pass in the stu1 object__init__Instantiate objects in the constructor

The above is the detailed content of the specific methods of python instantiating objects. For more information about how python instantiates objects, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

The specific method of python instantiation object
The specific method of python import library
End the method of running python
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(5)
Python method of parameter passing
Consolidate the foundation of Python (3)
The usage of wheel in python
Summary of logarithm method in Python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
Where is the pip path of python3
Method of installing django module in python
The essence of Python language: Itertools library
What are the advantages of python language
Example method of reading sql from python
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
Solve the conflict of multiple versions of python
Method analysis of Python calling C language program
Python realizes the development of student management system
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very
The latest method of installing Mongodb on Ubuntu 16.04
10. Object Oriented Python
Python writes the game implementation of fishing master
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
A brief summary of the difference between Python2 and Python3
Detailed explanation of the principle of Python timer thread pool
Solve the problem of python compiling and installing ssl
Detailed explanation of the implementation steps of Python interface development
How does python call the key of a dictionary
Python crawls the full set of skins of the king pesticide
How to understand the introduction of packages in Python