[Tutor] set locals

eryksun eryksun at gmail.com
Wed Dec 18 12:07:06 CET 2013


On Wed, Dec 18, 2013 at 5:40 AM, spir <denis.spir at gmail.com> 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'

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.


More information about the Tutor mailing list