RabbitInAHole
RabbitInAHole

Reputation: 581

Why isn't __dict__ a callable

Why are __dict__ and for example __unicode__ not both callable or the other way around?

Django tells me to set a unicode return as a method on an Model, for example:

class Obj(models.Model):
    def __unicode__(self):
        return 'hey'

>>> x = Obj()
<__main__.Obj object at 0x100703e10>
>>> x.__unicode__()
u'hello'
>>> x.__dict__
{}

It seems an odd discrepancy, can anyone explain it to me?

Upvotes: 4

Views: 837

Answers (2)

Fred Foo
Fred Foo

Reputation: 363818

The __foo__ convention is used for both methods and non-callable attributes in Python. __unicode__ is a method, __dict__ is not.

Upvotes: 4

Benjamin Peterson
Benjamin Peterson

Reputation: 20580

__dict__ is the dictionary of the instance, not a method.

Upvotes: 0

Related Questions