开发者

Singleton class in python

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

  cl开发者_StackOverflow中文版ass 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.

Also: Is there a simple, elegant way to define singletons?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜