Python singleton pattern -


Can anyone tell me why this is incorrect as a singleton pattern:

  Class preSingleton: def __call __ (self) Returns: Singleton = Precisionton (Single) = Singleton in fact Singleton A = Singleton () B = Singleton () Print a == B.A.Ver_In_A = 100 B. B = 'Hello' print A.var_in_b print b.var_in_a  

Edit: Print the code above:

  True 100  

very much Ywad

Part II

Maybe it's better?

Singleton (object): def __new __ (CLS): Returns CLS A = Singleton () B = Singleton () Print a == B.A.Ver_In_A = 100b.val_in_b = 'Hello' print a.var_in_b print b.var_in_a

Edit: Print the code above:

  True to 100  

Thanks again.

Making a really simple in singleton A. Python is a trick that the module should do your encapsulation for you and Do not create a category.

  • The module will be started only once
  • The module will not be started until it is first imported
  • Any attempt to import again will return an indicator to the current import

And if you want to pretend that the module is one example of a class, you can do the following

  Import some_module class SomeClass (object): def __init __ (self): self.singleton = some_module  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -