473,666 Members | 2,157 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 1167
"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.or g> 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.__clas s__.__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.__clas s__.__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.c om" <bo****@gmail.c om> 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.or g> 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.co m 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
1583
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
3287
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 a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
26
3448
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 record contains a header line for describing the sequence that begins with a '>' (right-angle bracket) followed by lines that contain the actual sequence data. Three example FASTA records are below: >CW127_A01...
19
36647
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 explanation, however, it may be too brief. Can anyone give me a more detailed explanation? Or give some referrences
121
10029
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
7
1220
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 part of my if statement. For example: if a = b b= 10
669
25860
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
21
2049
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 versions that work: nl = 0; nt = 0; ns = 0; nl = nt = ns = 0;
92
6187
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
8454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8362
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1778
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.