Article Directory
instance attribute, class attribute
In object-oriented development, an instance created using a class is an object, so is the class an object?
instance attribute
Objects created through classes are called instance objects, and object attributes are also called instance attributes, which record the data of each object, and the instance attributes of different objects with the same name, the recorded data are independent and do not interfere with each other.
class Person(object):
def __init__(self,name,age):
# 这里的name和age都属于是实例属性,每个实例在创建时,都有自己的属性
self.name = name
self.age = age
Every time an object is created, the object has its own name and age properties
p1 = Person('张三',18)
p2 = Person("李四",20)
class attribute
A class attribute is an attribute owned by a class object, which is shared by all instance objects of the class, and a class attribute can be accessed through a class object or an instance object.
class Dog:
type = "狗" # 类属性
Whether it is dog1, dog2 or Dog class, you can access the type attribute
print(Dog.type) # 结果:狗
print(dog1.type) # 结果:狗
print(dog2.type) # 结果:狗
scenes to be used
A class attribute is defined when an item of data recorded by an instance of a class is always consistent.
Instance attributes require each object to open up a separate memory space for it to record data, while class attributes are shared by all classes, occupying only one memory, saving more memory space.
important point:
- Try to avoid class attributes and instance attributes with the same name. If there is an instance attribute with the same name, the instance object will first access the instance attribute.
class Dog(object):
type = "狗" # 类属性
def __init__(self):
self.type = "dog" # 对象属性
create object
dog1 = Dog()
print(dog1.type) # 结果为 “dog” 类属性和实例属性同名,使用实例对象访问的是实例属性
- Class attributes can only be modified through class objects, not instance objects
class Dog(object):
type = "狗" # 类属性
dog1 = Dog()
dog1.type = "dog" # 使用实例对象创建了对象属性type
print(dog1.type) # 结果为 “dog” 类属性和实例属性同名,访问的是实例属性
print(Dog.type) # 结果为 "狗" 访问类属性
# 只有使用类名才能修改类属性
Dog.type = "土狗"
print(Dog.type) # 土狗
dog2 = Dog()
print(dog2.type) # 土狗
- Class attributes can also be set to private by adding two underscores in front. like:
class Dog(object):
count = 0 # 公有的类属性
__type = "狗" # 私有的类属性
print(Dog.count) # 正确
print(Dog.__type) # 错误,私有属性,外部无法访问。
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model