python-- class

Class definition

# Define a class 
class Luffy: 
    School = ' Luffy '     # data attribute 
    DEF Learn (Self):
         Print ( ' IS Learning ' )
     DEF EAT (Self):       # function attributes 
        Print ( ' IS eating ' ) 

Print (Luffy. The __dict__ )
 - --------------------------------------- 
{ ' __module__ ' : ' __main__ ' , ' School ' :'luffy', 'learn': <function Luffy.learn at 0x005C9B70>, 'eat': <function Luffy.eat at 0x005C9BB8>, '__dict__': <attribute '__dict__' of 'Luffy' objects>, '__weakref__': <attribute '__weakref__' of 'Luffy' objects>, '__doc__': None}

With different functions

After the completion of the class definition, it gave rise to the name space, and the function will produce when called

def test():
    a = 1
    print('hah')
    return a

print(test.__dict__)
----------------------------
{}

Change the class search deletions

# Internal name space 
Print (Luffy. The __dict__ ) 

# Charles 
Print (Luffy. The __dict__ [ ' School ' ])
 Print (Luffy.school) 

# increase 
Luffy.county = ' China ' 

# puncturing 
del Luffy.school 

# change 
Luffy.school = ' Oldboy '

 

Guess you like

Origin www.cnblogs.com/lalaxing/p/11404908.html