Attribute classes and objects

Class of relevant knowledge

In python2 the distinction:

Classic:

class School:
    pass

 

The new categories:

class School(object):
    pass

 

 In the above two are new class python3

Attributes:

  • Data attributes: is variable
  • Function attributes: is the function, commonly referred to in the object-oriented method

Note: classes and objects are used to access your property point

Class of property

That variable data attributes, and the class definition and function is similar and, in fact, can be used to understand the scope of a function property called class

Class data attributes:

class School:
    Teacher = "老王"
print(School.Teacher)

 

Function attribute class (commonly referred to as method):

class School: 
    Teacher = " Pharaoh " 
    DEF Examination (Self):
         Print ( " % S class taking a test " % Self) 
School.Examination ( " alex " )

 

View class properties:

View class properties in two ways:

  1. dir (class name) is isolated on a list of names
  2. Isolated .__ dict__ class name is a dictionary, key is the attribute name, value is the attribute value

# Note: The class name plus point to call his own property is actually a property to the dictionary to find things

School class: 
    Teacher = "Pharaoh" 
    DEF Examination (Self): 
        Print ( "% S class taking a test" Self%) 
Print Properties (dir (School)) # View class 
print (School .__ dict__) # View class property dictionary 
print (School.Teacher) # with print (School .__ dict __ [ " Teacher"]) call data attributes 
School.Examination ( "alex") # with School .__ dict __ [ "Examination" ] ( "alex") calls the function attributes

 Special class properties:

School class: 
    "" " 
    documentation 
    " "" 
    Teacher = "Pharaoh" 
    DEF Examination (Self): 
        Print ( "% S class taking a test" Self%) 
Print (School .__ name__) # View the class name 
print (School .__ doc__ ) # view the document 
print (School .__ base__) # View first class of the parent class 
print (School .__ bases__) # View all tuples parent class consisting of 
print (School .__ dict__) # View class properties 
print (School. __module__) # see which type module where 
print (School .__ class__) # School corresponding class instance (only the new class)

Object-related knowledge

Objects instantiated from the class is instantiated as a result of an object instance or referred

Instantiation:

School class: 
    "" " 
    documentation 
    " "" 
    Teacher = "Pharaoh" 
    DEF Examination (Self): 
        Print ( "% S class taking a test" Self%) 
School () 

# class name with parentheses is instantiated (understandable the return value is a function of a running example)

Examples of attributes:

School class: 
    "" " 
    It is a school class 
    " "" 
    Dang = "Dang" 
    # instantiated, init function not have a return value, the return value is null 
    DEF the __init __ (Self, name, addr, type): 
        self.mingzi # p1.mingzi name = name = 
        self.dizhi = addr addr = # p1.dizhi 
        self.leixing = = p1.leixing of the type of the type # 
    # test 
    DEF examination (Self): 
        Print ( "% S is test"% self.mingzi ) 
    # enrollment 
    DEF Recruit_students (Self): 
        Print ( "% S is enrollment" self.mingzi%) 
Person = School ( "Oldboy", "River", "private") 
person.Examination () 
"" "
1, essentially calling __init__ running instance of the process 
2, self instance itself is Person = 
. 3, class will automatically help return value __init__ 
. 4, only the data attribute instance, print (person .__ dict__)
5, Example Call the function attributes, actually calls a function of the class attribute, person.Examination ()   
6, Print (person.dang) with the same function scope, would go on in their scope can not find one find 
7, there are class data attributes and function attributes, attribute data examples only 
8 example call the function attributes will help you put .class instance itself to the function 
"" "

Guess you like

Origin www.cnblogs.com/NumerOne/p/11465040.html