2 Replies - 214 Views - Last Post: 23 August 2013 - 02:06 PM Rate Topic: -----

#1 stdq  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 261
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 stdq  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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 = '' )


View PostDblAAssassin, 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! =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1