473,511 Members | 15,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What a curious assignment.

[test 1]
class A: .... i = 1
.... a = A()
A.i 1 a.i 1 A.i = 2
A.i 2 a.i 2
[test2] class A: .... i = 1
.... a = A()
A.i 1 a.i 1 a.i = 2
A.i 1 a.i 2


Is there somthing wrong????

Nov 23 '05 #1
8 1156
"Ne******@gmail.com" <Ne******@gmail.com> writes:
[test 1]
class A: ... i = 1
... a = A()
A.i 1 a.i 1 A.i = 2
A.i 2 a.i 2
[test2] class A: ... i = 1
... a = A()
A.i 1 a.i 1 a.i = 2
A.i 1 a.i 2


Is there somthing wrong????


No. Reading a.i looks up i by checking the instance (a), then the
class (A), so a.i and A.i are the same thing. So changing A.i changes
the value seen by a.i. Binding a.i binds i to a, not A, so after the
binding, a.i and A.i are different things.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 23 '05 #2

Ne******@gmail.com wrote:
[test 1]
class A: ... i = 1
... a = A()
A.i 1 a.i 1 A.i = 2
A.i 2 a.i 2
[test2] class A: ... i = 1
... a = A()
A.i 1 a.i 1 a.i = 2
A.i 1 a.i 2


Is there somthing wrong????

I don't think so, the name binding stuff again. Good to know though.

Nov 23 '05 #3
"A.i" is a class attribute. "a.i" at first is the same as "A.i". Once
you set a.i = 2, you are actually creating a new data attribute called
i for the instance a. This happens on the fly. So then when you
reference a.i, it uses the instance data attribute, instead of the
class attribute.

This might make it more clear. Try:
a.f = 3
print a.f
Even though f is not declared in your class definition, the above code
still prints 3. Because it created the data attribute f on the fly.

Nov 23 '05 #4
Ne******@gmail.com wrote:
Is there somthing wrong????


Kids today, don't they learn about inheritence? :-)

Python's object model is that instances inherit both
methods and attributes from the class (and
superclasses). Methods are just a special case of
attributes: the method is a callable attribute.

When you reference an attribute, Python first checks
the instance by looking up instance.__dict__, and if
that fails, it looks up instance.__class__.__dict__.

(This is a simplification, e.g. it isn't exactly true
for objects with slots.)

For attribute lookup (that is, the attribute reference
is on the right hand side of an assignment), the lookup
may fail and so the class attribute may be retrieved.
This is by design.

For attribute assignment (that is, the attribute
reference is on the left hand side of an assignment),
the assignment will never fail.

(Again, ignoring slots and any other special cases I
have't thought of.)
--
Steven.

Nov 23 '05 #5

Steven D'Aprano wrote:
Ne******@gmail.com wrote:
Is there somthing wrong????


Kids today, don't they learn about inheritence? :-)

Python's object model is that instances inherit both
methods and attributes from the class (and
superclasses). Methods are just a special case of
attributes: the method is a callable attribute.

When you reference an attribute, Python first checks
the instance by looking up instance.__dict__, and if
that fails, it looks up instance.__class__.__dict__.

(This is a simplification, e.g. it isn't exactly true
for objects with slots.)

For attribute lookup (that is, the attribute reference
is on the right hand side of an assignment), the lookup
may fail and so the class attribute may be retrieved.
This is by design.

For attribute assignment (that is, the attribute
reference is on the left hand side of an assignment),
the assignment will never fail.

(Again, ignoring slots and any other special cases I
have't thought of.)

I believe he knows about inheritance, but not about the behaviour of
the assignment. In many other OO languages, I believe you cannot have
the same name for both instance variable and class variable. javascript
has similar behaviour.

Nov 23 '05 #6
Ne******@gmail.com wrote:
[test 1]
class A:
... i = 1
...
a = A()
A.i
1
a.i
1
A.i = 2
A.i
2
a.i
2
[test2]
class A:
... i = 1
...
a = A()
A.i
1
a.i
1
a.i = 2
A.i
1
a.i


2
Is there somthing wrong????


No.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 23 '05 #7
"bo****@gmail.com" <bo****@gmail.com> writes:
I believe he knows about inheritance, but not about the behaviour of
the assignment. In many other OO languages, I believe you cannot have
the same name for both instance variable and class variable. javascript
has similar behaviour.


I think more important is that in many languages you can't dynamically
add attributes to an object. So an attempt to bind a.i will either
fail, or be an assignment to A.i.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 23 '05 #8
On Wed, 23 Nov 2005 00:17:32 -0800, bo****@gmail.com wrote:

Steven D'Aprano wrote:
Ne******@gmail.com wrote:
> Is there somthing wrong????
Kids today, don't they learn about inheritence? :-)


[snip]
I believe he knows about inheritance,
Hence my smiley.
but not about the behaviour of
the assignment.
Which he now knows, based on trying it and seeing what happens.

In many other OO languages, I believe you cannot have
the same name for both instance variable and class variable. javascript
has similar behaviour.


I don't believe that is the problem. If that were the problem, the
original poster wouldn't have even tried to assign to the instance and the
class separately, surely.
--
Steven.

Nov 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1579
by: Elaine Jackson | last post by:
I'm new to Python, and I've noticed the following: >>> def f(a,b): a+=b >>> def g(a,b): a=a+b >>> p= >>> q= >>> r= >>> s=
28
3242
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
26
3435
by: Chris Lasher | last post by:
Hello, I have a rather large (100+ MB) FASTA file from which I need to access records in a random order. The FASTA format is a standard format for storing molecular biological sequences. Each...
19
36625
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
121
9908
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
7
1219
by: tshad | last post by:
I was curious if there is some reason why you don't need the "then" in the if test of VB.Net or is it just ASP.NET. I just noticed that I don't really need to explicitly put the word "then" as...
669
25398
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
21
2023
by: vlsidesign | last post by:
This syntax does not to work nl, nt, ns = 0; The only one that get's initialized is ns. nl and nt because they don't initialize seem to get some junk from memory. I have done these two...
92
6118
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.