attribute
1. Class attribute
a. Variables directly defined in a class are class attributes
b. How to use: call in the way of 'class.'
c. When to use: attributes are not different because of different objects
2. Object properties
a. Defined in the form of 'self. Attribute name = value'__ init __ Method
b. Use as an 'object.'
c. When to use: attributes vary from object to object
class Rect: side = 4 # Class properties def __init__(self, width, height): self.width = width # Object properties self.height = height
3. Initial value of object attribute
1) Assign a fixed value
class Rect: side = 4 # Class properties def __init__(self): self.width = 3 # Object properties self.height = 4
2) Use parameters without default values
class Rect: side = 4 # Class properties def __init__(self, width, height): self.width = width # Object properties self.height = height
3) Parameters using default values
class Rect: side = 4 # Class properties def __init__(self, width=3, height=4): self.width = width # Object properties self.height = height
method
1. Object method
How to define: functions defined directly in a class
How to call: object
Features: there is a default parameter self. When calling with an object, it does not pass parameters and points to the current object.
When to use: to realize the function of a function, you need to use the object method
2. Class method
How to define: add decorator before defining function: @ classmethod
How to call: class
Features: there is a default parameter cls, which does not need to pass parameters when calling, and points to the current class.
When to use: when objects are not needed, classes are needed to implement functions
3. Static method
How to define: add @ staticmethod before defining a function
How to call: class
Features: no default parameters
When to use: neither objects nor classes are required to implement the function of a function
class Test: def func1(self): print('Object method') @classmethod def func2(cls): print('Class method') @staticmethod def func3(): print('Static method') test = Test() test.func1() # Object method Test.func2() # Class method Test.func3() # Static method
4. Add, delete, modify and query object attributes
class Poker: def __init__(self,color,num): self.color=color self.num=num def show_card(self): print(f'{self.color}{self.num}') poker =Poker('heart','K') poker.show_card() # Add object properties poker.age=18 print(poker.age) # 18 # Delete object properties del poker.color # print(poker.color) #report errors poker.age=21 print(poker.age) # 21
inherit
1. Succession
Inheritance is to let subclasses directly own the properties and methods of the parent class
Subclass - successor
Parent - inheritee
2. How to create inheritance relationship
class Class name(Parent class): Class description document Class content class Person: def __init__(self): self.name = 'Xiao Ming' self.age = 18 self.gender = 'male' class Student(Person): def __init__(self): super().__init__() self.subject = 'Python' pass student = Student() print(student.name, student.age, student.gender) # Xiao Ming 18 male print(student.subject) # Python
3. Subclass addition
1) Add class properties and methods
Define a new class directly in a subclass
2) Add object properties
In subclass__ init __ Use super () inside__ init __ () call the initialization method of the parent class
Method calls in class:
First, check whether there is a corresponding method in the current class. If there is a direct call, it depends on whether there is a corresponding method in the parent class,
An error will not be reported until the object class is not found
time module
1.time() - get the current time (the timestamp is returned)
Time stamp: time is expressed by time difference (from 0:00:00:00 GMT on January 1, 1970, in seconds)
# 2021/10/29/19/19 t=time.time() # 1635506350.5123148
2.localtime() - get the local time of the current time (return the time structure)
Localtime (timestamp) - converts the timestamp to the structure time corresponding to the local time
t2=time.localtime() print(t2) ''' time.struct_time(tm_year=2021, tm_mon=10, tm_mday=29, tm_hour=19, tm_min=20, tm_sec=52, tm_wday=4, tm_yday=302, tm_isdst=0) ''' t2=time.localtime(1625006350) print(t2) ''' time.struct_time(tm_year=2021, tm_mon=6, tm_mday=30, tm_hour=6, tm_min=39, tm_sec=10, tm_wday=2, tm_yday=181, tm_isdst=0) '''
3. Conversion between structure time and string
Time.strftime (time format, time structure)
t3=time.strftime('%Y year%m month%d day %H:%M:%S',t2) print(t3) # June 30, 2021 06:39:10 t3=time.strftime('%Y-%m-%d %p %I:%M:%S',t2) print(t3) # 2021-06-30 AM 06:39:10 t3=time.strftime('%m-%d %A',t2) print(t3) # 06-30 Wednesday t3=time.strftime('%B %A',t2) print(t3) # June Wednesday t3=time.strftime('%Y year%m month%d day',t2) print(t3) # June 30, 2021
Time.strptime (string time, time format)
t3_str='2012 March 17' t3=time.strptime(t3_str,'%Y year%m month%d day') print(t3) ''' time.struct_time(tm_year=2012, tm_mon=3, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=77, tm_isdst=-1) '''
4. Convert structure time to timestamp
Mktime (structure time)
''' t3: time.struct_time(tm_year=2012, tm_mon=3, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=77, tm_isdst=-1) ''' t4=time.mktime(t3) print(t4) # 1331913600.0