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

Random recommended