An empty object with dynamic attributes (expando)

D

dmtr

How can I create an empty object with dynamic attributes? It should be
something like:

But this doesn't work. And I have to resort to:

Is there a one-liner that would do the thing?

-- Cheers, Dmitry
 
A

Alf P. Steinbach

* dmtr, on 03.06.2010 23:00:
How can I create an empty object with dynamic attributes? It should be
something like:


But this doesn't work. And I have to resort to:


Is there a one-liner that would do the thing?
</example>

But I feel that that is an abuse of the language, and that an Expando class like
you show is far better.

Thanks for the class name suggestion, by the way -- I've wondered what to call
such a class, and now it's clear. :)


Cheers & hth.,

- Alf
 
S

Steven D'Aprano

How can I create an empty object with dynamic attributes? It should be
something like:


But this doesn't work. And I have to resort to:


Is there a one-liner that would do the thing?

Why does it have to be a one-liner? Is the Enter key on your keyboard
broken?

You have a perfectly good solution: define a class, then instantiate it.
But if you need a one-liner (perhaps to win a game of code golf), then
this will work:
 
D

dmtr

Why does it have to be a one-liner? Is the Enter key on your keyboard

Nah. I was simply looking for something natural and intuitive, like: m
= object(); m.a = 1;
Usually python is pretty good providing these natural and intuitive
solutions.

You have a perfectly good solution: define a class, then instantiate it.
But if you need a one-liner (perhaps to win a game of code golf), then
this will work:

Heh. Creating it dynamically. Ace. ;)

-- Cheers, Dmitry
 
T

Terry Reedy

Nah. I was simply looking for something natural and intuitive, like: m
= object(); m.a = 1;
Usually python is pretty good providing these natural and intuitive
solutions.

As far as I can think of now, one cannot add attributes to *any*
builtin-class instance, but can add attributes to any user class which
does not have them disabled.
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
[].a = 3
AttributeError: 'list' object has no attribute 'a'[]
3

Terry Jan Reedy
 
A

Alf P. Steinbach

* Terry Reedy, on 05.06.2010 03:01:
Nah. I was simply looking for something natural and intuitive, like: m
= object(); m.a = 1;
Usually python is pretty good providing these natural and intuitive
solutions.

As far as I can think of now, one cannot add attributes to *any*
builtin-class instance, but can add attributes to any user class which
does not have them disabled.
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
[].a = 3
AttributeError: 'list' object has no attribute 'a'[]
3

Terry Jan Reedy

You can add attributes to functions.

I'm not sure, but I think it was you who once provided me with a reference to
the relevant PEP (thanks!), so I guess it just slipped your mind on a late
Friday night. :)

See my earlier reply in this thread regarding that.

Regarding user defined classes, I gather that by "have them [attributes]
disabled" you're including the case of a class with slots?

There was an article recently in DDJ where the author seemed to be unaware of
this, but I'm not going to pay for the privilege of commenting on articles.


Cheers & hth.,

- Alf


PS: How come that when I post a blog entry proving mathematically that the
reader is really really smart, the number of views dropped like a stone? Huh.
But OK, it was just a late-night posting, I couldn't sleep so I posted a bit of
what I, late at night, thought was some funny or at least amusing philosophy...
 
T

Terry Reedy

As far as I can think of now, one cannot add attributes to *any*
builtin-class instance, but can add attributes to any user class which
does not have them disabled.
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
[].a = 3
AttributeError: 'list' object has no attribute 'a'
class L(list): pass
i = L(); i; i.a = 3; i.a
[]
3

Terry Jan Reedy

You can add attributes to functions.

I'm not sure, but I think it was you who once provided me with a
reference to the relevant PEP (thanks!), so I guess it just slipped your
mind on a late Friday night. :)

Right on both counts. Function attributes are exceptions, and they were
added after I learned Python.
Regarding user defined classes, I gather that by "have them [attributes]
disabled" you're including the case of a class with slots?

Yes. or custom __setattr__ or something.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top