1

A seemingly easy thing which i cant get around.

registry  =  {}

def register(cls):
    registry[cls.__clsid__] = cls
    print cls
    return cls

@register
class Foo(object):
    __clsid__ = "123-456"

    def bar(self):
        pass
c=Foo()
d=Foo()

e=Foo()

Output:

<class '__main__.Foo'>

Now i expect decorator to be called 3 times.Why has it been called only once.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
vks
  • 67,027
  • 10
  • 91
  • 124
  • So what are you trying to achieve here? A class object is created just once; you are not creating more classes, you are creating *instances*. Those `Foo()` calls don't re-execute the `class` statement. – Martijn Pieters Oct 20 '16 at 13:46

2 Answers2

4

A class decorator is applied when the class is created, not each time an instance is created.

The @register line applies to the class Foo(object): statement only. This is run just once, when the module is imported.

Creating an instance does not need to re-run the class statement because instances are just objects that keep a reference to the class (type(c) returns the Foo class object); instances are not 'copies' of a class object.

If you want to register instances you'll either have to do so in the __init__ or the __new__ method of a class (which can be decorated too). __new__ is responsible for creating the instance, __init__ is the hook called to initialise that instance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • But isnt `class` just a template which comes into existence when `object` is created – vks Oct 20 '16 at 13:47
  • 1
    @vks: no, a class is an object. Calling that object then produces an instance which keeps a reference to the class. Through that reference attributes are resolved, which is how methods are found, for example. – Martijn Pieters Oct 20 '16 at 13:48
  • hey http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python we have singleton decorator for class....which allows only one object...if decorator is called only once,then how does it manage this? – vks Nov 08 '16 at 15:11
  • The class decorator in that question doesn't return a class object. It returns a function instead. You don't *have* to return a class object from a class decorator, at all. You just have to return *something* that'll then be bound to the class name. – Martijn Pieters Nov 08 '16 at 15:15
  • ok but still when i call for new object it wont get called right? then how it is this getting handled? – vks Nov 08 '16 at 15:20
  • Please do read the code of the class decorator; the function that's returned would be called instead, and it is responsible for creating an instance (and it'll only create *one*). – Martijn Pieters Nov 08 '16 at 15:24
2

The class decorator is being applied to the class itself, and it is applied only once, when the class is defined. Basically, it processes the class definition and produces a new class.

So you'd only process it once.

Horia Coman
  • 8,681
  • 2
  • 23
  • 25