object() can't have attributes

Chris Angelico rosuav at gmail.com
Wed Dec 23 07:58:53 EST 2015


On Wed, Dec 23, 2015 at 11:46 PM, Neal Becker <ndbecker2 at gmail.com> wrote:
> Sometimes I want to collect attributes on an object.  Usually I would make
> an empty class for this.  But it seems unnecessarily verbose to do this.  So
> I thought, why not just use an Object?  But no, an instance of Object
> apparantly can't have an attribute.  Is this intentional?

Yes; there are other uses of object() that benefit from being
extremely compact. You can use types.SimpleNamespace for this job, or
you can create the empty class as you're describing. (Chances are you
can give the class a meaningful name anyway.)

ChrisA



More information about the Python-list mailing list