031 single case + job, 031 job
Class Person: _ v = None @ classmethod def get_instance (cls): if cls. _ v: return cls. _ v else: cls. _ v = Person () return cls. _ v # create object obj = Person without using class. get_instance () print (obj)
It seems quite different from Java, which makes me feel a little strange. What I can be sure of is the above. The Singleton mode cannot be implemented, and I will add it later.
Supplement:
I checked it and found it was wrong. I felt a problem. This is not the case for the found python2. * Singleton.
Python2. * type change, but an error will be reported. I will not analyze this error and it is complicated.
class Singleton(object): def __new__(cls,*args,**kwargs): if not hasattr(cls,'_inst'): cls._inst = super(Singleton,cls).__new__(cls,*args,**kwargs) ###### TypeError: object() takes no parameters return cls._instif __name__ == '__main__': class A(Singleton): def __init__(self,s): self.s = s a = A('apple') b = A('banana') print(id(a), a.s) print(id(b), b.s)
It may be better to use the top one. I'll take a look at other tutorials. Then add
Assignment: Course Selection System (My Back Cover/cover your face)
Roles: schools, students, courses, Lecturers
Requirements:
1. Create two schools in Beijing and Shanghai
2. Create three courses for linux, python, and go. linux \ py is available in Beijing, and go is available in Shanghai.
3. Course inclusion, cycle, price, create course through school
4. Create a class through the school, and associate the relevant courses and lecturers in the class
5. When creating a student, select a school and associate the student with a class.
5. Associate the instructor role with the school,
6. Two Role interfaces are provided.
6.1 student view, you can register, pay tuition, select a class,
6.2 In the instructor view, the lecturer can manage his/her own class, select a class in class, view the list of class students, and modify the score of the managed students.
6.3 Management View, creating lecturers, creating classes, and creating courses
7. The data generated by the above operations are serialized and saved to the file through pickle.
Source documentation