473,789 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>.<attrib ute>, 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 1834
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*st atement*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.net com.com> wrote:
On Thu, 09 Sep 2004 16:59:24 -0700, anon <an**@anon.ne t> declaimed the
following in comp.lang.pytho n:

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
3236
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 there are no assignment expressions in Python, I have to use a temp var. That's a little more messy, but bearable:
5
5483
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 reference. However, when the object is a number, the story is so different. I don't understantd for what reason Python deals with them differently. The example is as follows: >>> a = >>> b = a
7
3672
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
166
8680
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
1481
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 usage before creation. Check this out: >>> class foo(object): .... I = 1 .... def __init__(self): .... print self.__dict__ .... self.I += 1
13
1951
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 embedded development forum: ----
18
6785
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, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
2
3285
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 attributes successfully (ManagementKey, ManagementProbe, ManagementBind...). The only one attribute I am having problem with is ManagementCreate. As I understand from the documentation, it is supposed to support creation of new instances of a...
0
9663
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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
10193
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
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 project—planning, coding, testing, and deployment—without 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...
0
6761
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
5415
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...
1
4089
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 we have to send another system
3
2906
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.