Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Programming languages > problem with classes
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 23rd October 06:31
cfgauss
External User
 
Posts: 1
Default 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]
  Reply With Quote


  sponsored links


2 24th October 08:56
bruno modulix
External User
 
Posts: 1
Default problem with classes



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 'onurb@xiludom.gro'.split('@')])"
  Reply With Quote
3 25th February 05:14
bruno modulix
External User
 
Posts: 1
Default problem with classes


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 'onurb@xiludom.gro'.split('@')])"
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666