Python (object instance attribute)

Scope of the problem category

The reference (.) Is a partial, inside the class will not exceed the scope of the class body, there is no reference (.) Is global

# - * - Coding: UTF-. 8 - * - 
wheel = 'wheel' 
class CAR: 
    'This is a car class' instructions # class 
    wheel = 'rubber' 
    Engine = 'engine' 
    DEF the __init __ (Self, License, Brand , price): # initialize must be so many (__init__), automatic return 
        # generate self Dictionary 
        self.Licenses = License 
        self.brands = Brand 
        self.prices =. price 
        Print ( 'I am a global', wheel, 'I am a local ', car.wheel) 
    DEF Transport (Self): 
        Print (' pull goods --- --- ') 
    DEF manned (Self): 
        Print (' --- --- manned ') 
    reference method # with 
    def colors (Self, color): 
        Print ( '% S --- S --- color is%'% (self.Licenses, color)) 

CAR1 CAR = ( '123456', 'public' '100000')

Modify the properties of the instance (by instance name) and the attribute class (with instance names) to distinguish

# - * - Coding: UTF-. 8 - * - 
class CAR: 
    'This is a car class' instructions # class 
    wheel = 'rubber' 
    Engine = 'engine' 
    DEF the __init __ (Self, License, Brand,. Price): # must be initialized so many (__init__), return automatically 
        # self generated dictionary 
        self.Licenses = License 
        self.brands = Brand 
        self.prices. price = 
    DEF Transport (self): 
        Print ( 'pull cargo --- ---') 
    DEF Manned (Self): 
        Print ( 'manned --- ---') 
    # parameterized method 
    DEF colors (Self, color): 
        Print ( '% S --- color is% s ---'% (self. the Licenses, Color)) 

CAR1 CAR = ( '123456', 'mass', '100,000') 
# change merely examples of 
car1.wheel = 'wheels. 1' 
Print (CAR.__dict__)
print(car1.wheel)
# Class is changed 
car.wheel = 'wheels 2' 
Print (CAR .__ dict__ magic)

Example Modify the modified class member variables

# - * - Coding: UTF-. 8 - * - 
class CAR: 
    'This is a car class' instructions # class 
    wheel = 'rubber' 
    Engine = 'engine' 

    S = [ 'Q', 'W'] 

    DEF the __init__ (self, License, brand, price ): # must be initialized so many (__init__), automatically return 
        # self generated dictionary 
        self.Licenses = License 
        self.brands = Brand 
        self.prices. price = 
    DEF Transport (self): 
        Print ( '- - pull goods --- ') 
    DEF manned (Self): 
        Print (' manned --- --- ') 
    reference method # with 
    DEF colors (Self, color): 
        Print ( "% color --- s. --- is S% '% (self.Licenses, Color)) 

CAR1 CAR = (' 123456 ',' mass', '100,000') 
# reassigned to the instance variable 
# car1.s = [1,2,3]
# print(car.s)
Print # (car1.s) 
# modifications are class variables, not properties 
car1.s.append ( 'E') 
Print (car.s) 
Print (car1.s) 
Print (CAR1 .__ dict__ magic)

  

Guess you like

Origin www.cnblogs.com/2018-1025/p/12019545.html