# Problem with Instances of a Class

Page 1 of 1

## 2 Replies - 252 Views - Last Post: 23 August 2013 - 02:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=327566&amp;s=96f16a1d1e30a6ca7d0360c9b14d0d2e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stdq

Reputation: -1
• Posts: 66
• Joined: 15-May 12

# Problem with Instances of a Class

Posted 23 August 2013 - 01:44 PM

Hi, everyone. I created a module with a class to represent an integer with many digits:

```class HugeInteger:
listOfDigits = []

def __init__( self, digitsAsString = '1' ):
if digitsAsString != '1':
for i in range( len( digitsAsString ) ):
self.listOfDigits += digitsAsString[ i ]
else:
self.listOfDigits += [ 1 ]

def display( self ):
for i in range( len( self.listOfDigits ) ):
print ( self.listOfDigits[ i ], end = '', sep = '' )
```

Then I created another file to test the module:

```import HugeIntegerModule

n1 = HugeIntegerModule.HugeInteger()
n1.display()

print()

n2 = HugeIntegerModule.HugeInteger( '12341' )
n2.display()
```

It prints n1 and n2, but not correctly. n1 is printed correctly, but n2 is printed as 112341 instead of 12341. However, if I only create n2 and forget about n1, n2 is printed properly. What am I doing wrong? Thanks in advance!

Is This A Good Question/Topic? 0

## Replies To: Problem with Instances of a Class

### #2 DblAAssassin

• D.I.C Regular

Reputation: 34
• Posts: 267
• Joined: 11-May 13

## Re: Problem with Instances of a Class

Posted 23 August 2013 - 02:03 PM

I'm still working on fully understanding classes, so my reply may or may not be too helpful.
Is it possible that on line 4, it show the one as a string, is that intentional or not?
Also what are you supposed to be printing on line 6 of you second block? You shouldn't need to print anything as the .display function covers that.

That's all I got, I apologize if I'm not any help. Classes are my definite weakness in Python.

### #3 stdq

Reputation: -1
• Posts: 66
• Joined: 15-May 12

## Re: Problem with Instances of a Class

Posted 23 August 2013 - 02:06 PM

Found the problem! I had to start the empty list inside the __init__method:

```class HugeInteger:
def __init__( self, digitsAsString = '1' ):
self.listOfDigits = []
if digitsAsString != '1':
for i in range( len( digitsAsString ) ):
self.listOfDigits += digitsAsString[ i ]
else:
self.listOfDigits += [ 1 ]

def display( self ):
for i in range( len( self.listOfDigits ) ):
print ( self.listOfDigits[ i ], end = '', sep = '' )
```

DblAAssassin, on 23 August 2013 - 02:03 PM, said:

I'm still working on fully understanding classes, so my reply may or may not be too helpful.
Is it possible that on line 4, it show the one as a string, is that intentional or not?
Also what are you supposed to be printing on line 6 of you second block? You shouldn't need to print anything as the .display function covers that.

That's all I got, I apologize if I'm not any help. Classes are my definite weakness in Python.

Thanks! But the problem was only with listOfDigits being a variable shared by all instances, because I was starting it in the body of the class. Thanks again! =)