Class Variable Access and Assignment

B

Bengt Richter

First of all, I've still not heard any sensible suggestions
about a saner behaviour for augmented assignment or for the
way Python searches the class scope after the instance scope.
A nit, but a sizeable one: For new-style classes, the class scope
is searched first for a descriptor that may trump the instance logic.
What do you suggest?

Today, x += n acts just as x = x + n if x is immutable.
Do you suggest that this should change?
A descriptor allows you to make it do as you like, so it's
a matter of discussing default behavior, not what you
are locked into (although costs re optimization could be a topic).
Today, instance.var will look for var in the class
scope if it didn't find it in the instance scope. Do
you propose to change this?
It is already changed, for new-style classes. It is only if
a data descriptor is NOT found in the class hierarchy that
an existing instance variable is accessed as "usual".
Or, do you propose that we should have some second order
effect that makes the combination of instance.var += n
work in such a way that these features are no longer
orthogonal?
I don't think he is proposing anything, just defending against
what he considers misinterpretations of what he is saying.
Given how hard it is to say ANYTHING and be understood EXACTLY,
this tends towards a pursuit of quantum nits ;-)
I suspect we all experience the emotions relevant to being misunderstood;
we just stop at different nit granularities (modulo horn locking ;-)

Regards,
Bengt Richter
 
B

Bengt Richter

Op 2005-11-04 said:
[...]
Sure, Python has evolved and grown for about 15 years, and
backward compatibility has always been an issue, but the
management and development of Python is dynamic and fairly
open-minded. If there had been an obvious way to change this
in a way that solved more problems than it caused, I suspect
that change would have happened already.

Fine I can live with that.
Amen ;-)

Regards,
Bengt Richter
 
C

Christopher Subich

Antoon said:
It is the combination of the two.

If python had chosen for an approach like function namespaces, the
problem wouldn't have occured either. What would have happened then
is that the compilor would have noticed the a.x on the right hand
side and based on that fact would then have deciced that all a.x
references should be instance reference (at least in that function
block). The a.x += ... would then result in an AttributeError being raised.

Problem:
"""
class B:
x = 1
classx = b()
instx = b()
instx.x = 5

def addtox(o):
o.x += 1

addtox(instx)
print B.x # 1
print instx.x # 6; we both agree on this one
addtox(classx) # You argue this should AttributeError
print B.x # ?! -- 1 currently, you argue 2 if no error
print class.x # we both agree 2, if no error
"""

a.x is /not/ a namespace issue at all; it's an attribute issue.

..x is not a name, it is an attribute. Python namespaces are lexically
scoped, not dynamically scoped; if, as you argue, .x should be a name in
a namespace, then you argue above that addtox in the above should work
on instx but fail on classx. But this /cannot be determined at compile
time/, because the attribute space is attached to the object passed in
as the parameter.

I repeat: this is not a name issue at all, it is an attribute issue.
Python's behaviour is counterintuitive from some angles, but it is the
only behaviour that is consistent with attributes in general, given the
signature of __iadd__ as-is.
You may prefer the current behaviour over this, but that is not the
point. The point is that resolution of name spaces does play its
role in this problem.

There are no name spaces.
It also has little to do with mutable vs immutable types.
Someone could implement an immutable type, but take advantage
of some implemtation details to change the value inplace
in the __iadd__ method. Such an immutable type would show
the same problems.

Immutable? I do not think that word means what you think it means.
 
A

Antoon Pardon

Op 2005-11-07 said:
Problem:
"""
class B:
x = 1
classx = b()
instx = b()
instx.x = 5

def addtox(o):
o.x += 1

addtox(instx)
print B.x # 1
print instx.x # 6; we both agree on this one
addtox(classx) # You argue this should AttributeError
print B.x # ?! -- 1 currently, you argue 2 if no error
print class.x # we both agree 2, if no error
"""

a.x is /not/ a namespace issue at all; it's an attribute issue.

.x is not a name, it is an attribute.

This may be a meaningfull distinction in the current implementation
but IMO it is less meaningfull to make such a distinction conceptually.

x is a name and proceeding it with a. is just a way of deciding in
which namespace the x is to be searched.
Python namespaces are lexically
scoped, not dynamically scoped; if, as you argue, .x should be a name in
a namespace, then you argue above that addtox in the above should work
on instx but fail on classx. But this /cannot be determined at compile
time/, because the attribute space is attached to the object passed in
as the parameter.

That depends on what you allow in the langauge. Allowing declarations
in the language could take care of that.

You could also decide scope per scope what is to be happend. If
somewhere in the scope there is a line a.x = ..., then within that
scope all 'a.x' refer to the object. In other scope a.x is decide
as it is now.
I repeat: this is not a name issue at all, it is an attribute issue.

Why make such a big distinction between the two?
Python's behaviour is counterintuitive from some angles, but it is the
only behaviour that is consistent with attributes in general, given the
signature of __iadd__ as-is.

If you mean the only behaviour that is consistent with the current
python attribute implementation. I can accept that. But as you have
worded it, it seems way to general, almost claiming that any language
that does it differently is not consistent. Maybe I misunderstood.
 
M

Magnus Lycka

Antoon said:
Fine that goes both ways. I don't mind not being taken serious by people
I have trouble taking serious my self. No doubt that goes for you too.

You know Antoon, these internet communities aren't really like
Speaker Corner in Hyde Park. You earn respect based on your merits,
not from the stubborn persistence in you arguments.

Steve has written a very good book on Python, he's worked a lot
with Python conferences, and helped people on comp.lang.python
for years etc. He has earned his respect.

You are fighting wind mills, bickering about things that you
don't have any solutions for. It's possible that you have just
not realized how Python handles objects, names and classes, but I
can't understand what you are trying to accomplish. What can you
possibly try to convey that you haven't already stated? It's
as if you've got stuck in this thread. In the real world, I
haven't heard of anyone ever having had problems with this.

Isn't it better to let it go and to spend your time on something
constructive?

I recently heard a priest say, that the difference between the
saint and the fanatic is that the fanatic tries to remove the
evil in the world, while the saint tries to remove the evil in
himself.

Just as introspection is useful both in fighting evil and in
Python programming, I think it's useful when we get into heated
discussions. I've always loved heated discussions, but over time,
I've come to realize that the best thing that can happen in such
a discussion is to realize that I was wrong. Then I've learnt
something! If I don't change my mind, I'm just standing still.
In that case, it might be useful for someone else, if she or he
learnt something from it, but the best thing is if I learn
something.

This thread is all to infected to lead to good things. Hopefully
you'll learn something about communication from it, but the price
has been higher than you might be aware of right now.
 
A

Antoon Pardon

Op 2005-11-08 said:
You know Antoon, these internet communities aren't really like
Speaker Corner in Hyde Park. You earn respect based on your merits,
not from the stubborn persistence in you arguments.

Steve has written a very good book on Python, he's worked a lot
with Python conferences, and helped people on comp.lang.python
for years etc. He has earned his respect.

So? Steve can be very good at explaining what python is and
how it behaves and at the same time have poor arguments why
this would be good langauge design.

So although he may have earned his respect in the first,
that doesn't mean I have to take him seriously in the
other.
You are fighting wind mills, bickering about things that you
don't have any solutions for.

People should know what they want. If one dares to propose
an alternative here, chances are that you get told to
search a language that behaves as you want, if you don't
you get blamed you don't have a solution.

The only acceptable behaviour, seems to keep quiet about
things where one thinks python could be improved.
It's possible that you have just
not realized how Python handles objects, names and classes, but I
can't understand what you are trying to accomplish. What can you
possibly try to convey that you haven't already stated? It's
as if you've got stuck in this thread. In the real world, I
haven't heard of anyone ever having had problems with this.

Well in the real world nobody seemed to have problems with
the lack of a condtional expression either. Each time someone
brought it up, they were told it wasn't necessary anyway and
how you could simulate it, with some caveat, by using 'and'
and 'or'.

Until it seems one of the developers got bitten by an elusive
bug caused by such a caveat and suddenly there will be a
condional expression in the next version.

So, that we haven't heard of anyone ever having a problem
with it doesn't contradict, that it may one day be the
cause of a very elusive bug.
This thread is all to infected to lead to good things. Hopefully
you'll learn something about communication from it, but the price
has been higher than you might be aware of right now.

Shrug, this is usenet, not my social life.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top