On 12/18/2013 12:07 PM, eryksun wrote:
On Wed, Dec 18, 2013 at 5:40 AM, spir <[email protected]> wrote:
     C.__setattr__(C, "baz", "BAZ")
which fails, for any reason, with
     TypeError: can't apply this __setattr__ to type object

You need __setattr__ from the metaclass:

     >>> class C: pass
     ...
     >>> type(C).__setattr__(C, "baz", "BAZ")
     >>> C.baz
     'BAZ'

Oh, that makes sense: so, __setattr__ on a class is for its instances, right? (thus, to set attrs on a class itself, we need ___setattr__ from its own class, if I understand rightly?)

You can bind the method like this:

     >>> C_setattr = type(C).__setattr__.__get__(C)
     >>> C_setattr('foo', 'bar')
     >>> C.foo
     'bar'

But just use built-in setattr(), really.

Really, yes!

Denis
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to