Article Directory
Object Oriented Basic Grammar
In Python, objects are almost everywhere, and we can use dir
built-in functions to view the methods in this object.
Define a simple class (contains only methods)
Object-oriented is a greater encapsulation, encapsulating multiple methods in a class, so that objects created through this class can directly call these methods!
define class
To define a class that only contains methods in Python, the syntax is as follows:
class 类名:
def 方法1(self,参数列表):
pass
def 方法2(self,参数列表):
pass
The definition format of the method is the same as the function learned before. The first parameter in the method must be self
, please remember it for now, and introduce it later self
. Class names must follow the upper camelCase naming convention.
Create an instance object
After a class definition is complete, use this class to create an object, the syntax is as follows:
对象变量名 = 类名()
first object oriented code
need
The kitten loves to eat fish, and the kitten wants to drink water.
analyze
- Define a cat class
Cat
. - Define two methods
eat
anddrink
. - As required - no properties need to be defined.
class Cat:
"""这是个猫类"""
def eat(self):
print("小猫在吃东西")
def drink(self):
print("小猫在喝水")
tom = Cat() # 创建了一个Cat对象
tom.eat()
tom.drink()
hello_kitty = Cat() # 又创建了一个新的Cat对象
hello_kitty.eat()
hello_kitty.drink()
Thinking:
tom
Is andhello_kitty
the same object?
use of self
Add properties to objects
Python supports dynamic attributes. When an object is created, 对象.属性名 = 属性值
you can easily add an attribute to the object by using it directly.
tom = Cat()
tom.name = 'Tom' # 可以直接给 tom 对象添加一个 name 属性
This method is convenient, but it is not recommended to add properties to objects in this way.
The concept of self
Which object calls the method, self
who is referred to in the method. Through self.属性名
the properties of this object can be accessed; through self.方法名()
the method of this object can be called.
class Cat:
def eat(self):
print("%s爱吃鱼" % self.name)
tom = Cat()
tom.name = 'Tom' # 给 tom 对象添加了一个name属性
tom.eat() # Tom爱吃鱼
lazy_cat = Cat()
lazy_cat.name = "大懒猫"
lazy_cat.eat() # 大懒猫爱吃鱼
Disadvantages of adding properties directly to objects
In the above code, we create the object first, and then add the name attribute to the object, but there will be problems in doing so.
tom = Cat()
tom.eat()
tom.anme = "Tom"
When the program runs, an error will be reported:
AttributeError: 'Cat' object has no attribute 'name'
Error message: 'Cat' object has no 'name' attribute.
In daily development, it is not recommended to directly add properties to objects outside the class. What properties should the object have, we should encapsulate them inside the class.
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