Hi,
Look at the follow command in python command line, See what's
interesting?:) class A:
i = 0 a = A() b = A() a.i = 1 print a.i, b.i
1 0
---------------------------------------
class A:
arr = [] a = A() b = A() a
<__main__.A instance at 0x00C96698> b
<__main__.A instance at 0x00CA0760> A
<class __main__.A at 0x00B314B0> a.arr.append("haha") print a.arr , b.arr
['haha'] ['haha'] a.arr = ["xixi"] print a.arr , b.arr
['xixi'] ['haha'] A.arr
['haha'] A.arr.append("xx") A.arr
['haha', 'xx'] a.arr
['xixi'] b.arr
['haha', 'xx'] b.arr.pop()
'xx' b.arr
['haha'] A.arr
['haha']
-------------------------------------
class X:
def __init__(self):
self.arr = [] m = X() n = X() m.arr.append("haha") print m.arr, n.arr
['haha'] [] 3 1618
Johnny Lee wrote: Hi, Look at the follow command in python command line, See what's interesting?:)
class A: i = 0 a = A() b = A() a.i = 1 print a.i, b.i 1 0
Quite what I would expect. First you declare i as being a *class*
attribute of A, with value 0. Then you create 2 instances a and b of A.
Then you add to a an *instance* variable named i (that then shadows the
class variable of the same name), with value 1. Then you print a.i,
wihci is the instance variable i of a, and b.i, which is the class
variable i of A, with value 0.
---------------------------------------
class A: arr = [] a = A() b = A() a <__main__.A instance at 0x00C96698> b <__main__.A instance at 0x00CA0760> A <class __main__.A at 0x00B314B0> a.arr.append("haha") print a.arr , b.arr ['haha'] ['haha']
Now you create a class A with a *class* variable arr which is an empty
list, and 2 instances a and b of A. Then you append to a.arr - which is
A.arr, so when you print a.arr and b.arr, you in fact print A.arr a.arr = ["xixi"]
Then you add an instance variable arr to a, shadowing A.arr
print a.arr , b.arr ['xixi'] ['haha']
So now you print a.arr and A.arr (accessed thru b)
(snip)
class X: def __init__(self): self.arr = [] m = X() n = X() m.arr.append("haha") print m.arr, n.arr
['haha'] []
Here you define a class X with an *instance* variable arr, and two
instances m and n of X, then append to m.arr, which of course has no
impact on n.
I dont see anything interesting nor problematic here. If you understand
the difference between class attributes and instance attributes, the
difference between mutating an object and rebinding a name, and the
attribute lookup rules in Python, you'll find that all this is the
normal and expected behavior.
Or did I miss something ?
--
bruno desthuilliers - is Python much more readable than Perl ???
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
bruno modulix wrote: I dont see anything interesting nor problematic here. If you understand the difference between class attributes and instance attributes, the difference between mutating an object and rebinding a name, and the attribute lookup rules in Python, you'll find that all this is the normal and expected behavior.
Or did I miss something ?
No, you didn't miss anything as I can see. Thanks for your help:)
Johnny Lee wrote: bruno modulix wrote:
I dont see anything interesting nor problematic here. If you understand the difference between class attributes and instance attributes, the difference between mutating an object and rebinding a name, and the attribute lookup rules in Python, you'll find that all this is the normal and expected behavior.
Or did I miss something ?
No, you didn't miss anything as I can see. Thanks for your help:)
You're welcome !-)
Ok, I guess all this is not that intuitive, specially when comes from
less dynamic languages. Python's object model is quite powerful, but one
need to have a good understanding of it to understand *why* it works
that way. There's an interesting slideshow about metaclasses and
descriptors that may help (if your brain is robust enough !-) : http://www.python.org/pycon/dc2004/p...sses-pycon.pdf
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nick Coghlan |
last post by:
Thought some folks here might find this one interesting. No great revelations,
just a fairly sensible piece on writing readable code :)
The whole article:...
|
by: ranjith g p |
last post by:
Greetings!!!
I ran the following simple string commands in Linux + Python and the results are:
# python
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
on linux2
Type "help", "copyright",...
|
by: Bruno R. Dias |
last post by:
Perhaps it would be interesting to program a virtual machine simulating
an ancient computer (such as the pdp-7). Then, it would be rather
interesting to code for it (porting gcc to it maybe?). I...
|
by: nufuhsus |
last post by:
Hello all,
First let me appologise if this has been answered but I could not find
an acurate answer to this interesting problem.
If the following is true:
C:\Python25\rg.py>python
Python...
|
by: Hendrik van Rooyen |
last post by:
I thought I would share this nasty little gotcha with the group.
Consider the following code fragment:
<start>
print 'starting kbd thread'
keyboard_thread = thread.start_new_thread(kbd_driver...
|
by: BEES INC |
last post by:
I've been awfully busy programming lately. My Django-based side
project is coming along well and I hope to have it ready for use in a
few weeks. Please don't ask more about it, that's really all I...
|
by: Gary Herron |
last post by:
BEES INC wrote:
Much simpler this way. This produces the number of whole start and the
number of half stars:
v = ... calculate the average ...
whole = int(v+0.25)
half =...
|
by: Nick Keighley |
last post by:
On 20 Jun, 10:29, onkar.n.maha...@gmail.com wrote:
no
C is statically compiled. By the time it executes everything
must be there and types are fixed. Dynamically linked libraries
can...
|
by: Mensanator |
last post by:
Beacuse in 2.6, Python apparently has fixed a discrepency that existed
in previous versions.
In the IDLE that comes with 2.5, typing "as", to wit "import random as
ran",
the words "import" and...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
| |