__getattribute__ for class object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sylvain Ferriol

    #1

    __getattribute__ for class object

    hello
    when i define __getattribute_ _ in a class, it is for the class instances
    but if i want to have a __getattribute_ _ for class attributes

    how can i do that ?

    sylvain
  • Paolino

    #2
    Re: __getattribute_ _ for class object

    Sylvain Ferriol wrote:[color=blue]
    > hello
    > when i define __getattribute_ _ in a class, it is for the class instances
    > but if i want to have a __getattribute_ _ for class attributes
    >
    > how can i do that ?
    >[/color]

    Skating on thin ice eh.Read something on metaclasses.


    class Meta(type):
    def __getattribute_ _(klass,attr):
    value=type.__ge tattribute__(kl ass,attr)
    print attr,'==',value
    return value

    class Foo(object):
    __metaclass__=M eta
    a=2

    Foo.a

    Paolino

    Comment

    • Dan

      #3
      Re: __getattribute_ _ for class object

      > > but if i want to have a __getattribute_ _ for class attributes[color=blue]
      >
      > Read something on metaclasses.[/color]

      Depending on what you want to do, it might be better to use properties
      instead:

      class Meta(type):
      x = property(lambda klass: 'Called for '+str(klass))

      class Foo(object):
      __metaclass__=M eta

      print Foo.x

      --
      Do I know what's in this bill? Are you kidding? Only God knows...
      - U.S. Senator Robert Byrd, when asked if he knew the
      contents of a $520 billion, 4000-page spending bill


      Comment

      • Steven Bethard

        #4
        Re: __getattribute_ _ for class object

        Dan wrote:[color=blue]
        > Depending on what you want to do, it might be better to use properties
        > instead:
        >
        > class Meta(type):
        > x = property(lambda klass: 'Called for '+str(klass))
        >
        > class Foo(object):
        > __metaclass__=M eta[/color]

        Also worth noting that you can inline the metaclass if you don't need it
        anywhere else, e.g.:

        class Foo(object):
        class __metaclass__(t ype):
        x = property(lambda klass: 'Called for '+str(klass))

        STeVe

        Comment

        Working...