Hulk
Hulk

Reputation: 34160

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

  class Test:
     name
     age

     def getobj(self):
        return (self.name+self.age)

    t= Test()

Upvotes: 0

Views: 1313

Answers (2)

Dan
Dan

Reputation: 41

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.

Upvotes: 2

gruszczy
gruszczy

Reputation: 42168

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?

Upvotes: 7

Related Questions