A.x vs. A["x"]

M

Martin Drautzburg

This has probably been asekd a million times, but if someone could give
a short answer anyways I's be most grateful.

What is it that allows one to write A.x? If I have a variable A, then
what to I have to assign to it to A.x becomes valid?

Or even further: what do I have to do so I can write A.x=1 without
having done anything magical for x (but just for A)? I know you can do
this with classes, but not with plain objects, but why is that so?
 
S

Steve Howell

This has probably been asekd a million times, but if someone could give
a short answer anyways I's be most grateful.

Not sure there is exactly a short answer, and I am only qualified to
maybe clarify some of the things you can and cannot do, not explain
the reasons they are so.
What is it that allows one to write A.x? If I have a variable A, then
what to I have to assign to it to A.x becomes valid?


Although not super concise, you'll find some good reading here:

http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

It maybe does not address your question exactly, but it might give you
insight into the overall philosophy.

Or even further: what do I have to do so I can write A.x=1 without
having done anything magical for x (but just for A)? I know you can do
this with classes, but not with plain objects, but why is that so?


Here are examples where adding attributes on the fly does not work:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more
information. Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'x'

Here are examples when you can assign new attributes:

Hope that helps or at least gets the discussion started.
 
T

Terry Reedy

This has probably been asekd a million times, but if someone could give
a short answer anyways I's be most grateful.

What is it that allows one to write A.x? If I have a variable A,

You do not really have a 'variable'. You have a name A bound to an
object that is an instance of class C = type(A).
what to I have to assign to it to A.x becomes valid?

If C has the appropriate special methods for get/set/del attribute, then
you can get/set/del attributes for instances of C (such as A).
If not, you cannot.

In general, builtin classes, including object, allow get but not set/del
on both the class and instances thereof.
Subclasses of object allow all three.
Or even further: what do I have to do so I can write A.x=1 without
having done anything magical for x (but just for A)?

Make A be a user-defined class or an instance thereof.
I know you can do this with classes, but not with plain objects, but why is that so?

You exact meaning here is not clear, but I suspect it is somewhat
incorrect, at least for built-in classes.

Terry Jan Reedy
 
M

Martin Drautzburg

You exact meaning here is not clear, but I suspect it is somewhat
incorrect, at least for built-in classes.

You're right. I used to think you could do this to classes:

G = Strum
G.x=1

But not to objects (instances):

g = Strum()
g.y = 2

But in fact both work. Thanks for clarifying
 
S

Steve Holden

Martin said:
You're right. I used to think you could do this to classes:

G = Strum
G.x=1

But not to objects (instances):

g = Strum()
g.y = 2

But in fact both work. Thanks for clarifying

Both work, in fact, because a class is (like almost any other Python
object), an instance of some class. In Python the classes that are
defined in the interpreter are usually called types, but since
"new-style objects" were introduced into Python in 2.2 you can define
your own types (and subclass the system types).

The name for the particular types whose instances are classes is
"metaclass". But once you realize that the "regular classes" you
already know about are just instances of their metaclass (the metaclass
<type 'Type'>, in many cases) it might seem less surprising.

There is a necessary but strange and surprising relationship between
types and objects that can be summarized as

isinstance(object, type) == isinstance(type,object) == True

Because the built-in types are implemented as a part of the interpreter
they are somewhat more restricted. Consequently not only can you not add
attributes to the classes, you can't add them to the instances either:
Traceback (most recent call last):
Traceback (most recent call last):

regards
Steve
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top