class
The definition of a class
Def __init__(self,name, HP,**occu**):# self self.name=name User self. HP = HP # the variable (name) is called as an attribute, Occu =occu** def print_role(self):# print('%s:%s ***%s***'%(self. Name,self. HP,** **)) def updaterName (self,newname): User1 =Player(' Tom ',100,'war') # Class instantiation the process of associating a user with a class that has characteristics of the class to a specific user is called class instantiation User2 =Player('jerry',100,'master') user1.print_role() ** user1.updaterName ('marry') user1.print_role()**Copy the code
How do I add properties and methods in bold
When an attribute of a class does not want to be accessed, add the __ example self.__name to the attribute and the variable will not be accessed by the instance: class encapsulation
Class inheritance
Feline (parent) –> Cat (subclass) The method by which cats inherit from felines
Class subclass name (parent class name) :
Def __init__(self, HP =100): self. HP = HP def run(self): Print (' move to a position ') class Animals(Monster): def __init__(self, HP =10): # Monster a1=Monster(100) print(a1. HP) print(a1. Run ()) a2=Animals(1) print(a1.hp) print(a2.run())Copy the code
polymorphism
When a parent class and a subclass define a method with the same name, the subclass’s call overrides the parent class
Class Monster(): def __init__(self, HP =100): self. HP = HP def run(self): self Def whoami(self): print(self, HP =10) def whoami(self): print(self, HP =10): # 'def whoami(self): print(' I am a subclass ') a3=Boss(10) a3.whoami()Copy the code
Result: I am a subclass
tisp:
① Use type area classification
Print (' a1 type is % s' % type (a1) print (' a2 type is % s' % type (a2)) print (' type a3 is % s' % type (a3))Copy the code
Results:
A1 is of type
The type of a2 is
A3 is of type
② Use isinstance to distinguish parent and child classes
print(isinstance(a2,Monster))
Copy the code
Result: True (False if not)
③ Use super when an attribute initialized in the parent class does not need to be defined again in the subclass
Super ().init(property name)
④ All lists, tuples, strings in Python… Both belong to classes whose parent is Object
⑤ Classes cannot be directly referenced and need to be instantiated
Customize the use of the with statement
With statement
with open() as f:
Copy the code
Customize the with statement
class Testwith(): def __enter__(self): print('run') def __exit__(self, exc_type, exc_val, exc_tb): Print ('exit') if exc_tb is None: # Use exc_tb to check for exceptions print(' normal end ') else: Print (" error %s" %exc_tb) with Testwith(): Exit print('Test is running') raise NameError('testNameError'Copy the code
multithreading
define
I understand multiple concurrent channels for solving problems such as multiple meal Windows
Def myThread(arg1, arg2): print('%s %s' (arg1, arg2)) for I in range(1, 6, 1): T1 = myThread(I, I + 1) threads import threading # import libraries import time from when the import current_thread # according to the current thread running state def myThread (arg1, arg2): Print (current_thread().getName(),'start') print('%s %s') arg2)) time.sleep(2) print(current_thread().getName(),'stop') for i in range(1, 6, 1): Increasing # loop five 1 t1 = threading. Thread (target = myThread, args = (I, I + 1)) # Thread target parameters: used to transfer function of the args: Print (current_thread().getName(),'end')Copy the code
Synchronization between multiple threads
object-oriented
Import current_thread class Mythread(threading.thread.thread):# threading.thread.thread () Def run(self) def run(self) Print (current_thread().getName(),'start') print('run') print(current_thread().getName(),'stop') a1=Mythread()# A1. Join () print(current_thread().getName(),'end')Copy the code
Results:
Thread-1 start
run
Thread-1 stop
MainThread end
Producer and consumer issues
The queue
Import queue q= queue.queue () # create queue q.ut (1) # add number to queue q.ut (2) q.git () # Add number to queue q.ut (2) q.git (Copy the code
From threading import Thread,current_thread,current_thread,current_thread Class ProducerThread(Thread): def run(self): Name =current_thread().getName() nums=range(100) Num =random. Choice (nums) # select nums. queue. Put (num)# select nums. queue. T =random.randint(1,3) time.sleep(t) print(' producer %s '% s '%(name,t)) class ConsumerThread(Thread): def run(self): name=current_thread().getName() global queue while True: Queue. Task_done () #task_done() print(' consumer %s consumes data %s'%(name,num)) T =random.randint(1,5) time.sleep(t) print(' consumer %s %s %s '%(name,t) print(' consumer %s %s %s '%(name,t)) c1=ConsumerThread(name='c1') c1.start() c2=ConsumerThread(name='c2') c2.start()Copy the code