Python notes 16 - object oriented Basics

Python notes

Object oriented Foundation

I object-oriented

Object oriented Encyclopedia

II Classes and objects

1. Classes and objects

Encyclopedia of classes and objects

  • class

Encyclopedia like

Class is a general term for a series of things with the same characteristics and behavior. It is an abstract concept. Class is used to create objects.
Characteristics are attributes and behaviors are methods

  • object
    Objects are real things created by classes.

2. Pair oriented implementation method

  • Define class
    The classes in Python 2 are divided into classic classes and new classes

    • grammar
    class Class name():
    	code
    	......
    

    Note: the class name must meet the identifier naming rules and follow the naming habit of large hump.

    • Examples
    class Dog():
    	def eat(self):
    		print('bone')
    
    • Classic class
      Classic class: a class that does not derive from any built-in type
      class Class name:
      	code
      	......
      
  • create object
    Objects are also called instances.

    • grammar
    Object name=Class name()
    
    • Examples
    class Dog():
    	def eat(self):
    		print('bone')
    # create object
    d1=Dog()
    # d1 object calls instance method
    d1.eat() # bone
    
  • self
    self refers to the object that calls the function.
    Examples

    class Dog():
        def eat(self):
            print('bone')
            print(self)
     # create object
     d1 = Dog() 
     print(d1) 
     # <__main__.Dog object at 0x0000010F1B4E9400>
    
     # d1 object calls instance method
     d1.eat()  
     # bone
     # <__main__.Dog object at 0x0000010F1B4E9400>
    
     d2 = Dog()
     print(d2) 
     # <__main__.Dog object at 0x0000010F1B4E94A8>
    
     # d2 object calls instance method
     d2.eat()  
     # bone
     # <__main__.Dog object at 0x0000010F1B4E94A8>
    

III Object properties

Attributes are characteristics.
Object properties can be added and obtained either outside the class or inside the class.

1. Add object attributes outside the class

  • grammar
Object name.Attribute name=value
  • Examples
d1.color='white'
d1.age=2

2. Get object properties outside the class

  • grammar
Object name.Attribute name
  • Examples
print(d1.color)
print(d1.age)

3. Get object properties in class

  • grammar
self.Attribute name
  • Examples
class Dog():
    def dog_info(self):
    	print(self.color)
    	print(self.age)
# create object
d1=Dog()

# Add instance properties
d1.color='white'
d1.age=2

d1.dog_info()
# whilte
# 2

IV Magic method

In Python__ xx__ () functions are called magic methods, which refer to functions with special functions.

1. init()
Function: initialize objects.

  • Parameterless__ init__ ()
class Dog():
    # Define initialization function
    def __init__(self):
        # Add instance properties
        self.color='white'
        self.age=2
    def dog_info(self):
        # Call instance property in class
    	print(self.color)
    	print(self.age)
# create object
d1=Dog()
# Call method
d1.dog_info()
# white
# 2

Note:__ init__ () method, which is called by default when creating an object and does not need to be called manually__ init__ The self parameter in (self) does not need to be passed by the developer, and the python interpreter will automatically pass the current object reference.

  • Parametric__ init__ ()

    • grammar
    def __init__(self,Attribute 1, attribute 2,...)
    	self.Attribute 1=Attribute 1
    	self.Attribute 2=Attribute 2
    	......
    
    • Examples
    class Dog():
        # Define initialization function
        def __init__(self,color,age):
            # Add instance properties
            self.color=color
            self.age=age
        def dog_info(self):
            # Call instance property in class
            print(self.color)
            print(self.age)
    # create object
    d1=Dog('white',2)
    d2=Dog('black',3)
    
    # Call method
    d1.dog_info()
    # white
    # 2
    
    d2.dog_info()
    # black
    # 3
    

2. str()
When print (object name) is used, the memory address of the object is printed by default. If the class is defined__ str__ () method, the return data in this method will be printed.

class Dog():
    # Define initialization function
    def __init__(self,color,age):
        # Add instance properties
        self.color=color
        self.age=age
    def __str__(self):
        return f'color: {self.color},age: {self.age}'

# create object
d1=Dog('white',2)

print(d1) # color: white,age: 2

3. del()
When an object is deleted, the python interpreter calls by default__ del__ () method.

class Dog():
    # Define initialization function
    def __init__(self,color,age):
        # Add instance properties
        self.color=color
        self.age=age
    def __del__(self):
        print(f'{self}Object deleted')
        
# create object
d1=Dog('white',2)

del d1 # <__ main__. Dog object at 0x000001ad5603b240 > object deleted

16
Levi_5

Keywords: Python Pycharm

Added by genie on Wed, 23 Feb 2022 02:37:25 +0200