python instance attributes of object-oriented and

First, the three major programming paradigms

1.1 process-oriented programming

Object-oriented programming is to be programmed as do one thing, according to the completion of the steps, each step is a process.

# Clean the room 
Print ( ' start sweeping ' ) 

DEF step_1 ():
     Print ( ' ready broom ' ) 

DEF step_2 ():
     Print ( ' clean room ' ) 

DEF Step_3 ():
     Print ( ' Saowan, back in the broom ground ' ) 

() step_1 
step_2 () 
Step_3 () 
execution results are as follows: 

begin sweeping 
prepare a broom 
to clean the room 
Saowan, the broom back into place

1.2 Functional Programming

Functional programming: a mathematical function + meaning = function functional programming language defined; popular terms, is to use functional programming language to implement mathematical functions. This function within the object is immutable, either parameter is a function, or return value is a function, not for and while loops, all by recursive loop to achieve, no variable assignment (that is, without variables to save the state), no assignment that is not changing.

1.3 Object-Oriented Programming

Object-oriented programming is to be treated as a thing, it is to set things can be programmed to do anything. two

DEF Personal (name, Gender, of the type):
     # human action 
    DEF The enroll (Personal):
         Print ( ' % S to go to school in Fujian ' % Personal [ ' name ' ])
     DEF Province (Personal):
         Print ( ' Peking University Economic and management Institute admitted [% S] ' % Personal [ ' type ' ]) 

    DEF the init (name, Gender, type): 
        personal1 = {
             " name " : name,
             " Gender " : Gender,
             "type": type,
            "enroll": enroll,
            "province":province
        }
        return personal1
    return init(name,gender,type)

p1=personal('czd','male','fzuniv')
print(p1)
p1['enroll'](p1)
p2=personal('dfz','female','bduniv')
print(p2)
p2['province'](p2)
View Code

Second, the basic understanding of the object class

Class: the same features and operation of a class of things is integrated together class, class is an abstract concept.

Object: class is created based on a specific thing (concrete existence), and also features integrated into one action.

The relationship between classes and objects: objects are generated by the class.

Example: From a production process class instantiation object is called, the result of the class instance is an object, or called an instance (instance = object)

Third, the knowledge classes and objects

3.1 class knowledge

There python2 Classic and new classes, python3 are in the new class.

The biggest difference is the new class and the classic class declaration that all new classes must inherit at least one parent

# Declare a class 

# Classic 
class Chinese:
     ' This is a Chinese class ' 
    Pass 
Print (Chinese) 

# instantiated in the end what did 
p1 = Chinese ()       # instantiation 
Print (p1)          # returns a result, a considerable in return 


# new-style class 
class Chinese (Object): # (parent)
     Pass 
Print (Chinese)

3.1.1 Properties

Class is used to describe a class of things, the object class refers to a class of things in the individual.

There are things we must attribute, attribute points:

    . A data attribute: that is variable

    b function attributes: is the function, commonly referred to in the object-oriented method

Summary: data attribute that is variable and function definitions of class and extremely similar, in fact, can be used to understand the scope of a function of class property call.

3.1.2 View class properties

Defined class attributes stored in the end to where? There are two ways to view

     dir (class name): a list of names is isolated

    Class name .__ dict__: isolated is a dictionary, key to the property name, value is the attribute value

3.2 object-related knowledge

class Japanese: 
    Party = ' Democratic Party ' 
    DEF  __init__ (Self, name, Age, Gender): 
        self.mingzi = name 
        self.nianling = Age 
        self.xingbie = Gender
     DEF sui_di_tu_tan ():
         Print ( ' towards that .... phlegm ' )
     DEF cha_dui (Self):
         Print ( " % S found in front of " % self.mingzi) 

P1 = Japanese ( ' DSA ' , 16, ' MALE')            #----->__init__(self,name,age,gender)

print(Japanese.__dict__)
Japanese.sui_di_tu_tan()
Japanese.cha_dui(p1)

3.3 deletions attribute change search class

class Chinese: 
    Party = ' New People party ' 
    DEF  the __init__ (Self, name, Age, Gender): 
        self.mingzi = name 
        self.nianling = Age 
        self.xingbie = Gender
     DEF sui_di_tu_tan (Self):
         Print ( ' % S toward .. is phlegm .. ' % self.mingzi)
     DEF cha_dui (Self):
         Print ( " % S found in front of " % self.mingzi)
     DEF eat_food (Self, Food):
         Print (' % S eating S% ' % (self.mingzi, Food)) 

P1 = Chinese ( ' Zhiqiang ' , 23 is, ' MALE ' ) 
p1.sui_di_tu_tan () 
p1.eat_food ( ' Panji ' ) 


P2 = Chinese ( ' CAW ' , 25, ' FEMALE ' ) 
p2.eat_food ( ' Mad Monk ' ) 

# ------------ check 
# Print (Chinese.party) 

# ----------- --- change 
Chinese.party = ' the Guomindang ' 
Print(Chinese.party) 

 # ------------ increase 
Chinese.guojia = ' China ' 
Print (Chinese.guojia) 

#   ---------- delete 
del Chinese.party
 Print ( Chinese. __dict__ )

3.4 Examples of deletions attribute change search

class Chinese:
    country='China'
    def __init__(self,name):
        self.name=name
    def play_ball(self,name):
        print('%s正在打%s'%(self.name,ball))

p1=Chinese('czd')
print(p1.__dict__)

# -----------查看
print(p1.name)
print(p1.play_ball)

# -----------增加
p1.age=18
print(p1.__dict__)
 Print (p1.age) 

# ----------------------- modified 
Chinese.country = ' Japan ' 
Print (Chinese.country) 
p1.age = 20 is
 Print (P1. The __dict__ )
 Print ( ' - ----> ' , p1.age) 

# ------------ delete 
del Chinese.country
 Print (Chinese. __dict__ )
View Code

Fourth, the supplementary objects and instances

class Chinese:
    country='China'
    l=['a','b']
    def __init__(self,name):
        self.name=name
    def play_ball(self,name):
        print('%s正在打%s'%(self.name,ball))

p1=Chinese('czd')
print(p1.l)

p1.l=[1,2,3]
print(Chinese.l)       #Is changed to p1, Chinese.l constant 
Print (p1. The __dict__ ) 

p1.l.append ( ' C ' )          # increase class inside 
Print (p1. The __dict__ )
 Print (Chinese.l)
View Code

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/changzhendong/p/11260329.html