[Tutor] set locals

spir denis.spir at gmail.com
Wed Dec 18 12:16:27 CET 2013


On 12/18/2013 12:07 PM, eryksun wrote:
> 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'

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


More information about the Tutor mailing list