day 03 python-- advanced object-oriented programming

First, static methods and class methods

Static method

Needs to be modified by modifying an @staticmethod, the method does not require static definition of multiple parameters, and can be accessed through the object class.

 1 class PD(object):
 2     def __init__(self,a,b,c,d):
 3         self.a = a
 4         self.b = b
 5         self.c = c
 6         self.d = d
 7     @staticmethod
 8     def is_valid(a,b,c,d):
 9         for i in [b,c,d]:
10             if i != a:
11                 return False
12         else:
13             return True
14     def area(self):
15         if res ==True:
16             area_ = self.a *self.b
17             return area_
18 pd = PD(2,2,2,2)
19 res = pd.is_valid(2,2,2,2)
20 if res == True:
21     print(pd.area())

Summary: no additional static method defined parameters, reference properties in a static method, it must be referenced by the object instance of the class

Class Methods

Class object is owned, decorator need @classmethodto identify which type of process for a class method, the first parameter must be a class object, generally clsas the first parameter (of course, other variables may be used as a first name a parameter, but most people are used to the 'cls' as the name of the first argument, it is best to use 'cls' a), able to access through the instance object and class objects.

. 1  class A (Object):
 2      DEF  the __init__ (Self):
 . 3          self.joker = 100
 . 4          
. 5      @classmethod
 . 6      DEF A (CLS):
 . 7          return CLS ()
 . 8      
. 9  # do not create an instance of A, A can be directly used call A 
10 Joker = Aa ()
 . 11  Print (Joker)
 12 is joker2 = A ()
 13 is  Print (joker2

 

Defaults

. 1  DEF say (Message, Times =. 1 ):
 2      Print (Message * Times)
 . 3  
. 4 say ( ' the Hello ' ) # default value is (Hello,. 1)
 . 5 say ( ' World ' ,. 5) # after change is (world , 5)

Summary: You may want to make some parameters optional and use default values, you do not want to avoid the situation provides a value to them. The default parameter values can be effective in helping to resolve this situation. You can attach an assignment operator (a function definition =) to specify a default value is a string or parameters.

Timestamp

1 from time import time,localtime,sleep
2 print(localtime(time()))

Second, the relationship between class and class

inherit

Inheritance refers to the relationship between class and class is a way to create a new class, the new class can inherit one or more parent classes, parent class can be called a base class or super class, derive a new class called class or sub-class, inheritance and single inheritance and multiple inheritance.

By inheritance to establish a relationship between the derived class and the base class, it is a 'yes' relationship, such as the white horse is a horse, man is an animal. When there between classes many of the same features, extract these common features make the base class, inheritance is better, such as the teacher is a person, who is a student

Single inheritance

E.g:

 1 class S(object):
 2     def __init__(self):
 3         self.a = 2
 4         self.b = 4
 5         
 6     def sum(self):
 7         SUM_ = self.a + self.b
 8         return SUM_
 9         
10         
11 class L(S):
12     def __init__(self):
13         S.__init__(self)
14     def p(self):
15         res = self.sum()
16         print(res)
17 
18 z = L()
19 z.p()

So that in the class A test1 into text in

(1)from test1 import A

(2)import test1

         test1.A ()

In order to test the output of only one result: if _ _name_ _ = = "_ _main_ _"

Function closure

1 def foo():
2     def bar():
3         return "hello"
4     return bar
5 
6 f1=foo()
7 print(f1())

List of Formula

Advantages: fast calculation, as have all-time loaded into memory, the data volume is not too large for the case 10000-2000-
disadvantages: memory for

a = [x for x in range(1000) if x % 2== 0]

Builder

Advantages: save memory
disadvantages: slow calculation as to generate

1 a = (x for x in range(100000000000) if x % 2== 0)
2 for i in range(100):
3     print(next(a))

Decorator

. 1  DEF Joker (FUNC):
 2  
. 3 DEF Warp (N1, N2, N3):
 . 4  
. 5 NUM = N1 + N2
 . 6  
. 7 return FUNC (0, NUM, N3)
 . 8  
. 9 return Warp
 10  
. 11  
12 is  
13 is # decorator summing the first two digits, multiplied by the third parameter function itself and this
 14  
15  @Joker
 16  
. 17  DEF the sUM (num1, num2, num3):
 18 is  
. 19 Print (num1, num2, num3)
 20 is  
21 is Print (num2 * num3)
22 
23 
24 
25 SUM(10,2,3)

Create a virtual environment

  1. conda create --name env_name python3.7 create a virtual environment
  2. conda activate env_name into the virtual environment
  3. conda env list to view the virtual environment
  4. pip freeze to view the current environment own installation package
  5. conda deactivate exit the virtual environment
  6. conda env remove --name env_name delete virtual environment

Guess you like

Origin www.cnblogs.com/hisashi-mitsui/p/11322115.html