Getting started with Python day9

Keywords: Python

inherit

  Inheritance? Something inherited from your elders
The question is: what did you inherit when you inherited?
  Class inheritance:
What is in the class? Variables and methods
class son(father)
This can even just write pass
But if the method in father wants to pass parameters to object variables
Pass parameters in son()

You can inherit a lot from an object__ xxxx__ Method of
For example__ new__ Create an object  __ init__ Initialize an object
 

The parent class inherited   Subclasses don't inherit
object default inheritance   Do not write
def __add__(self, other)#self +other
__ sub__ subtraction
__ mul__ multiplication
__ truediv__ division
__ le__   lt   gt   Less than or equal to
__len__
__str__
__dir__
__ 

[difference between str and repr]
str   Human readable
repr programmer readable

super (B, self) transmission   type, object-or-type
The function sefl determines the order of inheritance
    B to determine the search location   , Start after B

def _print_gender   Underline front   Privacy protection
  I can't get it out when others
_ xxx agreement   Private variables in python
__ XXX: don't write it in the back__ xxx__python reserved naming method
__ xxx: name rewriting in interpreter: definition__ python will automatically rename the variables and methods of xxx
                                  Change to_ ClassName__xxx:
      eg. There is a method method1 in the parent class
                  There is also a method in the subclass, method 456
class Parent:
  def  _ _ method1(self):   # Now this is called_ Parent_ _method1()
       print
class son(Parent):
    def _ _ Method 1 (self) is called_ son_ _method1()

When you use it, son_ Parent_ _ method1()

closure

Closure characteristics:
1 nested functions
2 the inner function references the variables of the outer function
3 the inner function is returned to the outer function as a return value
 
function . _ _code_ _.co_varnames look at the variables in the code block
                                .co_

globals() returns a dictionary containing the global variables of the current scope
locals() returns a dictionary containing the local variables of the current scope, also known as local variables
nonlocal variable name   Define a free variable
 

decorator
Use the principle of closure to make a decorator
Write a closure function  
@Decorator
def func()
  Where is the class variable
    Automatic (self)
I don't want to use self
Just
Static method @ staticmethod
Class method @ classmethod  

 work

  1. Define a parent class:
Requirement: it contains three object variables, and one of them is used_ name
Define a method: name using_ name
Define a subclass to inherit the upper parent class: and define a method (_) with the same method name as the parent class
Instantiate objects of subclasses
Access band_ Object variable for
Accessing _inthe parent class_ xxx method
Accessing _insubclasses_ xxx method

class Father:
    def __print(self):
        print("father")
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        self.arg2 = arg2
        self._arg3 = arg3

class Son(Father):
    def __print(self):
        print("son")

data = Son(1, 2, 3)
data._Father__print()
data._Son__print()


2. What is closure?
Characteristics of closures?
Define closures. The completed functions are: pass in a number, sum and output
For example: input 10 - > output 10
Incoming 15 - > output 25
Incoming 20 - > output 45

def sum_data():
    result = []
    def inner(value):
        result.append(value)
        print(sum(result))
    return inner

result = sum_data()
result(10)


3. Define a decorator: print the time spent running the function
You get a time before execution
Execution function
Gets a time after the function is executed
To find the time difference
time module

import time
def print_time(func):
    def inner(*args, **kwargs):
        old_time = time.time()
        result = func(*args, **kwargs)
        func_name = str(func).split(' ')[1]
        print('{} use time: {}s'.format(func_name, time.time() - old_time))
        return result

    return inner
@print_time
def func():
    time.sleep(3)
func()


4. Define a class:
Requirement: include an object attribute and use_ (single underline) named
Define a class method (decorator)
Define a static method (decorator)
Defining delegate properties (three class decorators): accessible_ The value of the named property
Can be modified_ The value of the named property
Can delete_ Named properties
Execution:
Instance an object
Call class methods (class and object)
Call static methods (class and object)
Object. Delegate properties
Object. Delegate attribute = value
del object. Delegate properties

 

class Computer:
    def __init__(self, brand, cpu, ram):
        self.brand = brand
        self.cpu = cpu
        self.ram = ram
        self._gpu = None
    @classmethod
    def class_method(cls):
        print("classmethod")
    @staticmethod
    def static_method(*args, **kwargs):
        print("staticmethod")
    @property
    def gpu_attr(self):
        return self._gpu
    @gpu_attr.setter
    def gpu_attr(self, value):
        self._gpu = value
    @gpu_attr.deleter
    def gpu_attr(self):
        del self._gpu
rog = Computer("Lenovo Savior", "intel7 10875H", "16G")
rog.class_method()
rog.static_method()
rog.gpu_attr = "RTX 2060"
print(rog.gpu_attr)
del rog.gpu_attr

Posted by kaumilpatel on Wed, 17 Nov 2021 07:02:17 -0800