Class definitions and object creation

Classes and Objects Introduction

Copy the code
That class category, type, object-oriented design is the most important concept, 

the object is a combination of characteristics and skills, and the category is similar to a series of objects and features a combination of skills 

in the real world: first, the object, and then there class, object is a concrete existence, while the class is just a concept, not real
 
# in the program, be sure that: first, the definition of class, after class generated using the object 

PS: 
 1. features used to identify variables in the program, skills to use function identification 
 2. thus the most common class is nothing more than: the definition of variables and functions
Copy the code

The definition of class

Copy the code

class class name:
  content # class describes the attributes and skills 
  # description attribute with variable
  # describe the behavior of a function

# First class name written specification is intended to see to know the name of the name of a big hump nomenclature 
# hump word is capitalized, the big hump is the first letter capitalized, small hump is the first letter lowercase

# Note: 
  1. The class can have arbitrary python code that will execute in the class definition phase 
  2 and they may be a new name space used to store variable names and function names like, by class name .__ dict__ View 
  3. for the classical class for us (the new class is limited) by the name of the dictionary operations class name space, but provides us with a special python syntax 
  4. point is the syntax to access attributes defined in the class name, attributes are class: designed to access the property, the nature of the operation is __dict__
Copy the code

Attributes

Copy the code
 
  

Class has two attributes: data attributes and function attributes

1. The data class attribute is shared by all objects

2. The function attribute class is bound to the subject with

 

Properties can also be written in an object 

Object properties, each object is unique

If present in the same class attribute to access the object if there is no access to the object class and 

Copy the code

Deletions attribute change search

Copy the code
Increase property 
object variable name. Attribute name = attribute value 

to delete attribute 
del object variable name attribute name 

modify 
objects. Attribute = New Value 

View property access is the object of all the properties 
print (Object .__ dict__) 

class information access objects 
print (Object .__ class__)
Copy the code

Create Object

Copy the code
class class name:

  pass

# Create Object 
 Object Name = class name ()

Create Object -> name space of the calling class

Also called to create an object instance of the class, the class definition of a good, can create an instance of an object class according to the class

A class can build multiple objects, each object in memory is independent

The essence of the object function is to integrate data and process the data together, so to get an object on both his function data and processing data to be processed 

Copy the code

__init__ method

Copy the code
__init__ initialization method is called, is essentially a function of 

characteristics 1: when the object is instantiated, the init method of automatically 
Feature 2: automatically object as the first argument, parameter names bit self, self may be other name, but is not recommended to change the 

functions: user initial value assigned to the object   

# stressed: 
# 1, can have any python code within the process 
# 2, must not have a return value
Copy the code

python class built special properties

Copy the code
The name of the class name .__ name __ # class (string) 

document String class name .__ doc __ # class (comments) 

class name .__ first parent base __ # class 

class name .__ bases __ # class tuple of all the parent class consisting of 

class name dictionary .__ dict __ # kind attribute 

class name .__ __ # Module1 module where the class definition 

class name of class __ # .__ corresponding class instance (only the new class)
Copy the code

 

 
 

Classes and Objects Introduction

Copy the code
That class category, type, object-oriented design is the most important concept, 

the object is a combination of characteristics and skills, and the category is similar to a series of objects and features a combination of skills 

in the real world: first, the object, and then there class, object is a concrete existence, while the class is just a concept, not real
 
# in the program, be sure that: first, the definition of class, after class generated using the object 

PS: 
 1. features used to identify variables in the program, skills to use function identification 
 2. thus the most common class is nothing more than: the definition of variables and functions
Copy the code

The definition of class

Copy the code

class class name:
  content # class describes the attributes and skills 
  # description attribute with variable
  # describe the behavior of a function

# First class name written specification is intended to see to know the name of the name of a big hump nomenclature 
# hump word is capitalized, the big hump is the first letter capitalized, small hump is the first letter lowercase

# Note: 
  1. The class can have arbitrary python code that will execute in the class definition phase 
  2 and they may be a new name space used to store variable names and function names like, by class name .__ dict__ View 
  3. for the classical class for us (the new class is limited) by the name of the dictionary operations class name space, but provides us with a special python syntax 
  4. point is the syntax to access attributes defined in the class name, attributes are class: designed to access the property, the nature of the operation is __dict__
Copy the code

Attributes

Copy the code
 
 

Class has two attributes: data attributes and function attributes

1. The data class attribute is shared by all objects

2. The function attribute class is bound to the subject with

 

Properties can also be written in an object 

Object properties, each object is unique

If present in the same class attribute to access the object if there is no access to the object class and 

Copy the code

Deletions attribute change search

Copy the code
Increase property 
object variable name. Attribute name = attribute value 

to delete attribute 
del object variable name attribute name 

modify 
objects. Attribute = New Value 

View property access is the object of all the properties 
print (Object .__ dict__) 

class information access objects 
print (Object .__ class__)
Copy the code

Create Object

Copy the code
class class name:

  pass

# Create Object 
 Object Name = class name ()

Create Object -> name space of the calling class

Also called to create an object instance of the class, the class definition of a good, can create an instance of an object class according to the class

A class can build multiple objects, each object in memory is independent

The essence of the object function is to integrate data and process the data together, so to get an object on both his function data and processing data to be processed 

Copy the code

__init__ method

Copy the code
__init__ initialization method is called, is essentially a function of 

characteristics 1: when the object is instantiated, the init method of automatically 
Feature 2: automatically object as the first argument, parameter names bit self, self may be other name, but is not recommended to change the 

functions: user initial value assigned to the object   

# stressed: 
# 1, can have any python code within the process 
# 2, must not have a return value
Copy the code

python class built special properties

Copy the code
The name of the class name .__ name __ # class (string) 

document String class name .__ doc __ # class (comments) 

class name .__ first parent base __ # class 

class name .__ bases __ # class tuple of all the parent class consisting of 

class name dictionary .__ dict __ # kind attribute 

class name .__ __ # Module1 module where the class definition 

class name of class __ # .__ corresponding class instance (only the new class)
Copy the code

 

Guess you like

Origin www.cnblogs.com/wkq0220/p/11241448.html