473,386 Members | 1,864 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,386 software developers and data experts.

problem with classes

I am having a strange problem with classes. I'm fairly sure the
problem is with classes, anyway, because when I re-write the program
without them, it works like I'd expect it to.

When I run this program, at first, L[0].z[1] is 0, because z=[0,0].
But after I run that loop to assign different values to L[1].z[0] and
L[1].z[1], somehow the value of L[0].z[1] has changed to 1. I have no
idea why this is happening, can anyone help me?

class Stuff:
z=[0,0]

L = [Stuff(),Stuff()]
print "L[0].z[1]=%i" % L[0].z[1]
for j in range(2):
L[1].z[j] = j
print "L[0].z[1]=%i" % L[0].z[1]

Aug 26 '05 #1
1 934
cfgauss wrote:
I am having a strange problem with classes. I'm fairly sure the
problem is with classes, anyway, because when I re-write the program
without them, it works like I'd expect it to.

When I run this program, at first, L[0].z[1] is 0, because z=[0,0].
But after I run that loop to assign different values to L[1].z[0] and
L[1].z[1], somehow the value of L[0].z[1] has changed to 1. I have no
idea why this is happening, can anyone help me?

class Stuff:
z=[0,0]


This is a class attribute (shared by all instances of Stuff), not an
instance attribute.

To define instance attribute, the syntax is:

class Stuff(object):
def __init__(self):
self.z = [0, 0]

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Aug 26 '05 #2

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

Similar topics

18
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
4
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
5
by: Alexis | last post by:
Hello, I have a set of classes I created from schema files using the xsd.exe tool. I'm using namespaces in the clases ( I had to because I have some classes with the same name but not the same...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
6
by: Florian Lindner | last post by:
Hello, I have these class declarations class CBauteil { private: int m_iNumber; int m_iWeight; public: int getNumber(); void setNumber(int);
12
by: Nathan Sokalski | last post by:
I have several CustomControls that I have written for my project. However, when I try to compile I recieve the following warning & errors: Warning 32 Could not resolve this reference. Could not...
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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
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
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...

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.