Basic knowledge of python class 1

Posted by mothermugger on Wed, 31 Jul 2019 19:04:07 +0200

10. Class

Definition of Class 10.1

Process Oriented: Write code from top to bottom according to business logic, emphasizing that each step must be completed in person

Object-oriented: encapsulate data and functions together, reduce duplication of code, absolutely believe in a person, many operations I let him execute instead of me.

Expressions of Category 10.2

class Name: class Name, Big Hump Naming Rule, class Name Following class
 Classes have attributes: a set of data (static data)
Method (function) of a class: Some operations (behavior) on it

Example:
Dog:
Class name: DOG
 Attributes: Variety, Coat Color, Sex, Name, Tail, Leg Number
 Method: Running, barking, biting, demolishing and eating

Category 10.3 Cases

class DogInfo(object):
	def __init__(self,leg):
		# Initialization operations, defined by attributes, instance attributes
		self.leg = leg
	def bark(self):
		print('Dogs bark')
		
Note:
1. self, which represents the instance object, is called automatically in the process of use.
2. In init, the magic method defines the attributes of instances.
3. DogInfo() creates instantiated objects. Note that the init method will be called automatically at this point. If init requires parameters other than self, it needs to be passed directly in () at this point.

10.4 Create instances through classes

Instance = class name ()
Note:
If init needs to be passed, then () fill in the parameters of init

10.5 Call instance attributes

10.5.1 is inside the class

class DogInfo(object):
	def __init__(self,leg):
		# Initialization operations, defined by attributes, instance attributes
		self.leg = leg
	def bark(self):
		# Calling instance attributes in methods
		print(self.leg)
		print('Dogs bark')
        
Note:
The instance attribute invoked in the method, self. attribute name invocation, because self points to the instance

10.5.2 Called outside the class

class DogInfo(object):
	def __init__(self,leg):
		#Initialization operations, defined by attributes, instance attributes
		self.leg = leg
	def bark(self):
		#Calling instance attributes in methods
		print(self.leg)
		print('Dogs bark')
		
//Call outside the class:
1,Create an instance
dog = DogInfo(4)
2,Call properties
dog.name


//Note:
//Outside the class, invoke instance attributes by creating objects

10.6 Call methods through instances

10.6.1 Calls methods outside the class

class DogInfo(object):
    def __init__(self,leg):
        #Initialization operations, defined by attributes, instance attributes
        self.leg = leg
    def bark(self):
        #Calling instance attributes in methods
        print(self.leg)
        print('Dogs bark')

dog = DogInfo(4)
dog.bark()

//Note:
//Call through an instance
//If the method needs to pass parameters, write the arguments directly in the method ().

#### 10.6.2 Call methods inside classes

class DogInfo(object):
    def __init__(self,leg):
        #Initialization operations, defined by attributes, instance attributes
        self.leg = leg
    def bark(self):
        #Calling instance attributes in methods
        print(self.leg)
        print('Dogs bark')

    def use_bark(self):
        self.bark()
        

dog = DogInfo(4)
dog.use_bark()

//Note:
//Calls within classes are also made by self. method name ()

10.7 Dynamic addition of temporary attributes and acquisition

class DogInfo(object):
    def __init__(self,leg):
        #Initialization operations, defined by attributes, instance attributes
        self.leg = leg
    def bark(self):
        #Calling instance attributes in methods
        print(self.leg)
        print('Dogs bark')


dog1 = DogInfo(4)
dog1.name = 'Xiao Bai'
print('Access to Temporary Properties',dog1.name)

dog1.bark()

//Note:
1 On the Difference between Instance Attribute and Temporary Attribute
	//Instance attributes are provided by instance objects created through the current class
	//Temporary attributes, which are owned by an instance, can be dispensed with by other instance objects.
2,In writing web Sometimes, for convenience's sake, add

10.8 init new method

@staticmethod # known case of __new__
def __new__(cls, *more): # known special case of object.__new__
    """ Create and return a new object.  See help(type) for accurate signature. """
    pass
    

def __init__(self): # known special case of object.__init__
    """ Initialize self.  See help(type(self)) for accurate signature. """
    pass

Note:

1. New method creates and returns a new instance object

2. Initialization of Instances by init Method

3. new first

10.9 str

class DogInfo(object):
    def __init__(self,leg):
        #Initialization operations, defined by attributes, instance attributes
        self.leg = leg
    def bark(self):
        #Calling instance attributes in methods
        print(self.leg)
        print('Dogs bark')


dog1 = DogInfo(4)
print(dog1)

//Exhibition:
<__main__.DogInfo object at 0x0000000002859C50>

Using str

class DogInfo(object):
    def __init__(self,leg):
        #Initialization operations, defined by attributes, instance attributes
        self.leg = leg
    def bark(self):
        #Calling instance attributes in methods
        print(self.leg)
        print('Dogs bark')
    def __str__(self):
        return 'Dog'


dog1 = DogInfo(4)
print(dog1)

//Exhibition:

//Dog

Note:

return string,''

Topics: Attribute