Python base (24) - instance attributes and object-oriented

Three programming paradigm: Programming Methodology

Process-oriented, functional programming, object-oriented (not the strongest method, which only the strongest)

Functional programming is a function of the mathematical sense, is a function of the parameters or return is a function, not the variable assignment, with a recursive loop, but not with the meaning of the programming function

Evolutionary programming, programming processes --- (bad maintenance, complex, unorganized unstructured) --- duplicate functional logic organized to form the structure and operation of --- attribute data objects grouped together, describes an object programming is object-oriented, object-oriented object is encapsulated private property and action, object or class to bind

All things can be used to describe the properties and actions

Not only is the class object-oriented functions can also be object-oriented, using the nested function implementation

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

Object: a specific thing is class-based and created a concrete existence, but also the integration of features and acts together, this is called object-oriented design

Object-Oriented Programming: OOP (object -oriented programming) is defined by way of example, the object class + object-oriented design to achieve

class:

When a class is instantiated to increase ()

Classic class name: class calss new name (name): There are new categories in succession in Python3

The property contains two classes: data property is property that is a function of variables in object-oriented method called, through point to visit, in fact, to find the point is to find a property by that function through the property dictionary, this point is equivalent to class. __dict __ [ 'attribute name'] () call content attribute dictionary

.__ dict__ class is the class attribute dictionary dir just put the name of the class dict the detailed contents of the corresponding dictionary

 Object:

 

class sth:
    def __init__(self,name,age):
        self.name=name
        self.age=age

s1=sth('alex','18')
print(s1.name)

self is an example of himself, init is the initialization function will automatically take parameters and returns the dictionary, self.name actually in the dictionary to find the key, when instantiated actually find dictionary keys, see below

 

class sth:
    def __init__(self,name,age):
        self.name=name
        self.age=age

s1=sth('alex','18')
print(s1.__dict__)

The result is

{ ' Name ' : ' Alex ' , ' Age ' : ' 18 is ' } # actually see is a dictionary data structure, but this type of data processing special classes automatically add a return value returned does not require

When __init__ scope of functions not found, it will come out a layer, so the call is to find examples of properties and methods in the whole class scope. Function attribute is scope belong to a class, the only instance of the attribute data

Dictionary class is larger than the range of examples, including the method of the class, so the class method should increase self, the instance name when the parameter passed in, so plus points are also examples of adjustable function method used in class , as long as the added self argument function method. Deep Thought, instantiated just did not bind initialization function approach, but only when used in the method will be called in, so save memory, you do not need to load all come in. Method in class design is an example of call is to call the class.

In fact, the first parameter is the type of automatic transmission, you can play other names, calling the class function automatically passed his first parameter

The first argument of all class functions must be self, class named camelCasing

Class CRUD deleted del

Examples of additions and deletions to change search does not affect the properties of the class, he was using his scope, if you define a variable when it did not increase self in his assignment which is not in its scope, is the entire function of the variables

= 12 is a
 class STH: 
    a = 14
     DEF  the __init__ (self, name, Age): 
        the self.name = name 
        self.age = Age
         Print (a)    # here is not a class variable, because no increase self format, without being given, when the call point is useless. it does not matter to refer to the class and 

S1 = STH ( ' Alex ' , ' 18 is ' )
 Print (sth.a)

 12 is 
14

Operation must example is the assignment is completed, the case where the append the like in the Examples without complete assignment list, only an effect on the properties of the class and instance variables can not be added

 

class sth:
    a=[14]
    def __init__(self,name,age):
        self.name=name
        self.age=age

s1=sth('alex','18')
# s1.a=['a','b']
s1.a.append('c')
print(s1.__dict__)
print(sth.__dict__)

 

Guess you like

Origin www.cnblogs.com/dayouge/p/11183934.html