[pyar] Ejecutar un método al finalizar la instanciación

John Rowland Lenton john.lenton en canonical.com
Lun Jul 12 20:37:07 ART 2010


On Mon, Jul 12, 2010 at 04:25:08PM -0300, Juan BC wrote:
> 
> agrego: googlee un cacho y todos me mandan a classmethod

si eso es verdad, están "todos" equivocados :)

Fijate en la documentación de __new__:

  object.__new__(cls[, ...])

    Called to create a new instance of class cls. __new__() is a static
    method (special-cased so you need not declare it as such) that takes
    the class of which an instance was requested as its first
    argument. The remaining arguments are those passed to the object
    constructor expression (the call to the class). The return value of
    __new__() should be the new object instance (usually an instance of
    cls).

si creés que la documentación pueda estar mal, fijate que el super(),
para un classmethod, se usaría así:

   super(A, cls).method(args...)

similar a como se llama para un método común,

   super(A, self).method(args...)


si todavía no te convencés, hacé


  class A(object):
    def foo(*a): pass

    @classmethod
    def bar(*a): pass

    @staticmethod
    def baz(*a): pass

y fijate qué devuelve A.foo, A.bar y A.baz. Comparalo con lo que te da
el A.__new__ implícito, y con el __new__ con @classmethod y etcétera.

HTH,
------------ próxima parte ------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20100712/15709ea8/attachment.sig>


More information about the pyar mailing list