Classes with initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mariano.suarezalvarez@gmail.com

    #1

    Classes with initialization

    Hi all,

    I'm currently using code similar to this:

    class ClassWithInitia lization(type):
    def __init__(cls, name, bases, dict):
    type.__init__(n ame, bases, dict)
    dict['__class_init__ '](cls)

    class A:
    __metaclass__ = ClassWithInitia lization

    def __class_init__( cls):
    cls.some_attrib ute = ...
    ...

    in order to get class attributes initialized (since the values of
    these attributes
    need non trivial work to be computed, putting the code that does that
    computation in the class scope ends up with the class having extra
    attributes---the `local' variables used in the computation of the
    values of class attribute; so I'm using __class_init__' s scope to
    contain those variables)

    I was wondering: is there a simpler approach to this?

    Also: can someone enlighten me as to when code in class scope is run,
    exactly?
    if a class A has a metaclass M, then M.__init__ does not seem to get
    the code in A's class scope in its arguments AFAICS, so I guess that
    code is run before the class is created?

    Cheers,

    -- m

  • kyosohma@gmail.com

    #2
    Re: Classes with initialization

    On Apr 9, 2:26 am, mariano.suareza lva...@gmail.co m wrote:
    Hi all,
    >
    I'm currently using code similar to this:
    >
    class ClassWithInitia lization(type):
    def __init__(cls, name, bases, dict):
    type.__init__(n ame, bases, dict)
    dict['__class_init__ '](cls)
    >
    class A:
    __metaclass__ = ClassWithInitia lization
    >
    def __class_init__( cls):
    cls.some_attrib ute = ...
    ...
    >
    in order to get class attributes initialized (since the values of
    these attributes
    need non trivial work to be computed, putting the code that does that
    computation in the class scope ends up with the class having extra
    attributes---the `local' variables used in the computation of the
    values of class attribute; so I'm using __class_init__' s scope to
    contain those variables)
    >
    I was wondering: is there a simpler approach to this?
    >
    Also: can someone enlighten me as to when code in class scope is run,
    exactly?
    if a class A has a metaclass M, then M.__init__ does not seem to get
    the code in A's class scope in its arguments AFAICS, so I guess that
    code is run before the class is created?
    >
    Cheers,
    >
    -- m
    As I understand it, class code doesn't get run until you create an
    instance of the class and call a method with that instance. I don't
    know how to answer your other question though.

    Mike

    Comment

    • Michele Simionato

      #3
      Re: Classes with initialization

      On Apr 9, 9:26 am, mariano.suareza lva...@gmail.co m wrote:
      Hi all,
      >
      I'm currently using code similar to this:
      >
      class ClassWithInitia lization(type):
      def __init__(cls, name, bases, dict):
      type.__init__(n ame, bases, dict)
      dict['__class_init__ '](cls)
      >
      class A:
      __metaclass__ = ClassWithInitia lization
      >
      def __class_init__( cls):
      cls.some_attrib ute = ...
      ...
      >
      in order to get class attributes initialized (since the values of
      these attributes
      need non trivial work to be computed, putting the code that does that
      computation in the class scope ends up with the class having extra
      attributes---the `local' variables used in the computation of the
      values of class attribute; so I'm using __class_init__' s scope to
      contain those variables)
      >
      I was wondering: is there a simpler approach to this?
      Yes, see http://www.phyast.pitt.edu/~micheles...itializer.html
      Also: can someone enlighten me as to when code in class scope is run,
      exactly?
      if a class A has a metaclass M, then M.__init__ does not seem to get
      the code in A's class scope in its arguments AFAICS, so I guess that
      code is run before the class is created?
      >
      Cheers,
      >
      __init__ is run after class creation. What does not work exactly?

      Michele Simionato

      Comment

      • Alex Martelli

        #4
        Re: Classes with initialization

        <kyosohma@gmail .comwrote:
        ...
        Also: can someone enlighten me as to when code in class scope is run,
        exactly?
        It's run as a part of the execution of the class statement.
        if a class A has a metaclass M, then M.__init__ does not seem to get
        the code in A's class scope in its arguments AFAICS, so I guess that
        code is run before the class is created?
        Right. Nutshell 2nd ed, p 83 under "The class statement":

        """
        The nonempty sequence of statements that follows the class statement is
        known as the class body. A class body executes immediately as part of
        the class statement's execution. Until the body finishes executing, the
        new class object does not yet exist and the classname identifier is not
        yet bound (or rebound). "How a Metaclass Creates a Class" on p. 118
        provides more details about what happens when a class statement
        executes.
        """

        The key to the latter part of the explanation (which actually starts on
        p. 117:-) is that the class body executes in a temporary dictionary, say
        d; that d is then used to help determine the meclass M (it may direct it
        by having a key '__metaclass__' ); then, d is passed as one of the three
        arguments to the call to M (preceded by the classname string and the
        tuple of the class's bases) -- lastly, the classname is bound or rebound
        to the result of that call.
        As I understand it, class code doesn't get run until you create an
        instance of the class and call a method with that instance. I don't
        No, this delay applies to code that's part of methods defined in the
        class, but not to code that appears directly in classbody. Easy to
        check:
        >>class Greet(object):
        .... print 'hello world'
        ....
        hello world
        >>>
        As you see, although no instance of the class has yet been created, the
        print statement that's directly in classbody has already run. If you
        want to check that it runs _before_ the metaclass gets called, set a
        custom metaclass that also does a print in its __init__, and see in what
        order the print statements execute...


        Alex

        Comment

        • Carl Banks

          #5
          Re: Classes with initialization

          On Apr 9, 3:26 am, mariano.suareza lva...@gmail.co m wrote:
          Hi all,
          >
          I'm currently using code similar to this:
          >
          class ClassWithInitia lization(type):
          def __init__(cls, name, bases, dict):
          type.__init__(n ame, bases, dict)
          dict['__class_init__ '](cls)
          >
          class A:
          __metaclass__ = ClassWithInitia lization
          >
          def __class_init__( cls):
          cls.some_attrib ute = ...
          ...
          >
          in order to get class attributes initialized (since the values of
          these attributes
          need non trivial work to be computed, putting the code that does that
          computation in the class scope ends up with the class having extra
          attributes---the `local' variables used in the computation of the
          values of class attribute; so I'm using __class_init__' s scope to
          contain those variables)
          >
          I was wondering: is there a simpler approach to this?
          You could put the computations right in the class, and del the extra
          variables when done.


          Carl Banks

          Comment

          Working...