current position:Home>First acquaintance with Python class
First acquaintance with Python class
2022-01-31 17:22:18 【A program cat】
「 This is my participation 11 The fourth of the yuegengwen challenge 15 God , Check out the activity details :2021 One last more challenge 」
Little knowledge points :
Object oriented and process oriented :::
(1) Process oriented ( Do as you please ): Cook rice , Wash the dishes , Wash the dishes , Chopping vegetables , Stir fry , Pots having dinner
(2) object-oriented ( Everything is done by objects ): Girl friend ( Cook rice , Wash the dishes , Wash the dishes , Chopping vegetables , Stir fry , Pots ) We directly having dinner
Take out having dinner
class ( abstract )
Class is an independent variable ( attribute / Method ) Space .( Encapsulate the common features of some things .)
An instance is also a space for storing variables independently .( Each instance is an independent variable space , The space between different instances is not visible )
Copy code
Class names should be capitalized , The instance name should be lowercase .
Instance name . Property name Call an instance property .
Class name . Property name Call the properties of the class encapsulated in the class .
Class coding style :
Class names should be named using the hump naming method , Capitalize the first letter of every word in the class name , Instead of using underscores . Instance name And module names are in lowercase , And underline the words .
For each class , You should include a docstring immediately after the class definition . This document string briefly describes the of the class function , And follow the formatting conventions used when writing the documentation string for the function . Each module should also contain a documentation string , What are the classes used for .
You can use blank lines to organize your code , But don't abuse it . In class , You can use a blank line to separate methods ; And in the module , You can use two blank lines to separate classes .
When you need to import both the module in the standard library and the module you wrote , Write to import the standard library module first import sentence , Again Add a blank line , Then write to import your own module import sentence . In containing multiple import Statement in the program , This makes it easier to see where the modules used by the program come from .
Definition :
class Class name :
pass
Copy code
1. for example :
class Person: # Define that the class name should be capitalized .
a = ' Two hands , Two legs ' # Attributes encapsulated in a class
# class , Is an independent storage variable ( attribute / Method ) Space .
wumou = Person() # Instance name = Class name () example
zhangmou = Person()
wumou.name = " Wu " # The characteristics of the instance Instance attributes ( The characteristics of an example , It's property )
wumou.age = 18
zhangmou.age = 20
print(Person.a) # Invokes the properties of the class
print(wumou.age) # Invoke instance properties
Copy code
The relationship between classes and instances :::
(1) class :::
It's an abstraction of a kind of thing , It's not real , Describe the commonalities of this kind of things .
for example :“ people ”,“ animal ”
(2) example :::
A specific individual of something , Is the concrete manifestation of this kind of things , It's real .
for example :“ Wu ” It's a specific person ,
“ zhang ” It's also a specific person .
2. Private property :
stay python There are two private properties in , Add an underscore before the attribute () And two underscores ()
An underscore can be accessed directly from the outside , The two underscores cannot be accessed directly outside .
An underline is just a simple mark .
Two are private properties , It can't be used directly .
python There is no absolute private property in the private property .
class Person:
_sex = " male "
__age = 18
wuhan = Person() # example
print(Person._sex) # Calling class properties
print(Person.__age) # Double underline will report an error , Double underlined variable names cannot be accessed directly
print(Person._Person__money) # This allows you to access double underlined properties
Copy code
stay python in ,_ and __ The use of is more of a norm / Appointment , There is no real limit to the purpose
Private properties in a defined class can also be inherited by subclasses
Little knowledge points :::
dir() You can view the method , You can put the class name inside .
Copy code
copyright notice
author[A program cat],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311722157771.html
The sidebar is recommended
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
guess what you like
-
leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
-
Python interface automation test framework (basic) -- basic syntax
-
Detailed explanation of Python derivation
-
Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
-
A brief note on the method of creating Python virtual environment in Intranet Environment
-
[worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
-
[Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
-
The unknown side of Python functions
-
Python based interface automation test project, complete actual project, with source code sharing
-
A python artifact handles automatic chart color matching
Random recommended
- Python crawls the map of Gaode and the weather conditions of each city
- leetcode 1275. Find Winner on a Tic Tac Toe Game(python)
- leetcode 2016. Maximum Difference Between Increasing Elements(python)
- Run through Python date and time processing (Part 2)
- Application of urllib package in Python
- Django API Version (II)
- Python utility module playsound
- Database addition, deletion, modification and query of Python Sqlalchemy basic operation
- Tiobe November programming language ranking: Python surpasses C language to become the first! PHP is about to fall out of the top ten?
- Learn how to use opencv and python to realize face recognition!
- Using OpenCV and python to identify credit card numbers
- Principle of Python Apriori algorithm (11)
- Python AI steals your voice in 5 seconds
- A glance at Python's file processing (Part 1)
- Python cloud cat
- Python crawler actual combat, pyecharts module, python data analysis tells you which goods are popular on free fish~
- Using pandas to implement SQL group_ concat
- How IOS developers learn Python Programming 8 - set type 3
- windows10+apache2. 4 + Django deployment
- Django parser
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
- apache2. 4 + Django + windows 10 Automated Deployment
- leetcode 1222. Queens That Can Attack the King(python)
- leetcode 1387. Sort Integers by The Power Value (python)
- Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
- Python object oriented programming 01: introduction classes and objects
- Baidu Post: high definition Python
- Python Matplotlib drawing contour map
- Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
- Python classic: explain programming and development from simple to deep and step by step
- Python implements URL availability monitoring and instant push