How to understand python objects

Class is an abstract concept, such as human beings, birds, and fruits. It is a general term that is not specific to a certain object;

Object (object, refers to a specific instance, instance);

Add an instance variable to the object

# Add a skills instance variable
p.skills =['programming','writing']print(p.skills)
# Delete the name instance variable of the p object, instead of deleting the variable in the class, create a new object, the name instance variable is still the default constructor.
del p.name
# print(p.name)Will report an error

Python allows access to class variables through objects, but if the program tries to assign values to class variables through objects, the nature will change at this time. Python is a dynamic language, and assignment statements often mean defining new variables. Therefore, if the program assigns a value to a class variable through an object, it is not actually a "class variable assignment", but a new instance variable is defined. For example, the following program.

classInventory:
 # Define two variables
 quantity =2000
 item ='mouse'
 # Define instance method
 def change(self,item,quantity):
 self.item = item
 self.quantity = quantity
# Create Inventory object
iv =Inventory()
iv.change('monitor',500)
# Access iv's item and quantity instance variables
print(iv.item) #monitor
print(iv.quantity) #500
# Access Inventotry's item and quantity class variables
print(Inventory.item) #mouse
print(Inventory.quantity) #2000

View extension:

Python's object orientation can be simply understood as the things you deal with are "objects". The variable refers to an object, the variable name is an object, and the related concept is the namespace. A class represents a class of things and is an object. The instance under the class is the specific manifestation of the class, which is equivalent to an individual with a certain characteristic, and these are all objects.

So far this article on how to understand python objects is introduced. For more related python objects how to understand content, please search the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future!

Recommended Posts

How to understand python objects
How to understand variables in Python
How to understand python object-oriented programming
How to understand global variables in Python
How to comment python code
How to uninstall python plugin
How to use python tuples
How to understand the introduction of packages in Python
How to understand a list of numbers in python
python how to view webpage code
How to use python thread pool
How to write python configuration file
How to wrap in python code
How to save the python program
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to install Python on CentOS 8
How to solve python dict garbled
How to view errors in python
How to write return in python
How to view the python module
How to clear variables in python
How to use SQLite in Python
How to verify successful installation of python
How to make a globe with Python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to save text files in python
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
01. Introduction to Python
How to write try statement in python
How to define private attributes in Python
R&D: How To Install Python 3 on CentOS 7
How to add custom modules in Python
How to process excel table with python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to debug python program using repr
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to get started quickly with Python
How to enter python triple quotation marks
Introduction to Python
How to add background music in python
How to represent relative path in python
How to upgrade all Python libraries on Ubuntu 18.04
How to use the round function in python
How to convert web pages to PDF with Python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
An article to understand the yield in Python
How to use the format function in python
How to play happily with Python3 on Ubuntu