For a class in python how to implement singleton properties to it.Please provide an example for the following class.What i am basically trying to understand is that if an instance of the class exist then it should return the existing instance else create an instance of that class
class Test:
name
age
def getobj(self):
return (self.name+self.age)
t= Test()
You should not implement singleton as a class. Use a module, that works great as a singleton.
I personally wouldn't use a singleton design pattern on such a class, as with a singleton you are ensuring that there is, and only ever will be one of them. Why would you only ever want one Employee?
You could say have it on say, an employeeManager, or an employeeList, even if i'm not a huge fan of having it on those either.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.