473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

attribute assignment effects all class instances

I'm aware that you can assign a value to an attribute in all class
instances by assigning to <Class>.<attribute>, however, my case is
slightly different and bizarre.

In module node:

top, left, mid, right = range(4)

class Node:

def __init__(self, ntype = None, function = None, symbol = None,
meta = None, adjacent = [None, None, None, None]):
self.type = ntype
self.function = function
self.symbol = symbol
self.meta = meta
self.adjacent = adjacent
Here's the wierd thing, in another module I have:

node.adjacent[left] = y

where node is an instance of the Node class. This statement assigns
all Node instances the value y to attribute adjacent[left]. This seems
very wrong, unless python resolves names in a case-insensitive way.

When I try the alternate:

node.adjacent = [None, y, None, None]

I get the behavior I expect (ie. only the instsance node gets its
attribute set)

Can anyone shed any light on this?

Thanks.
--Greg
Jul 18 '05 #1
2 1815
anon wrote:

Here's the wierd thing, in another module I have:

node.adjacent[left] = y

where node is an instance of the Node class.**This*statement*assigns
all Node instances the value y to attribute adjacent[left].**This*seems
very wrong, unless python resolves names in a case-insensitive way.
When you assign the default value for adjacent in the constructor definition
as you do, that list is created only one time, and the same list is passed
as the default value for each instance of Node.

Thus, when you alter the list, the change shows up in all instances, because
they are all using the same list.
When I try the alternate:

node.adjacent = [None, y, None, None]

I get the behavior I expect (ie. only the instance node gets its
attribute set)


In the second case you have not altered the existing list, but rather
reassigned your attribute to a new list. Hence, only the node you are
working on changes. At that point all of the nodes *except* the one you
just changed are using the same list for the "adjacent" attribute.
Hope that helps,
Jeffrey
Jul 18 '05 #2
In article <n4********************************@4ax.com>, Dennis Lee
Bieber <wl*****@ix.netcom.com> wrote:
On Thu, 09 Sep 2004 16:59:24 -0700, anon <an**@anon.net> declaimed the
following in comp.lang.python:

In module node:

class Node:

Here's the wierd thing, in another module I have:

node.adjacent[left] = y

where node is an instance of the Node class. This statement assigns


I'd recommend some name changes somewhere... Too many "nodes"
here...

Your "another module" had to have done an "import node" (since
that is the name you claim at the top). You then had to perform
something like: "node = node.Node()" to create the instance, but that
would also supersede the module name node...


Dennis--
I actually imported the module as "from node import *", so I haven't
run into a name conflict (apparently). Though I see your point for
naming things differently.

Peter--
Sorry for my miswording of assignment to shared class data.

Everyone--
Thank you very much for your assistance. I was unaware that the
default arguments were created only once and then reused for each
instance. The situation makes complete sense now.

--Greg
Jul 18 '05 #3

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
13
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
2
by: =?Utf-8?B?dXJrZWM=?= | last post by:
I am trying to create an in-process WMI provider using System.Management.Instrumentation namespace. For testing I use a simple class as a wrapper for FileInfo class. I have been able to use all...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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 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.