firefly2517
firefly2517

Reputation: 115

PYTHON: AttributeError - calling function in a member function of a class

I'm having trouble with classes in Python 2.7:

First, I really do not know how to use the __init__ properly, so I have just used a dummy call to print. What should I do instead?

Second, I would like the member function readAnyFormat to call a few functions (later I will create a kind of case statement). My attempt produced an AttributeError. How do I do this correctly?

My class is as follows:

class ArangeData:

        def __init__(
                    self):

            print ' '

        def readAnyFormat(
                    self,
                    config = True,
                    mypath='text.txt',
                    data_format='ASCII',
                    data_shape='shaped'):

            #Here the function should be called:'
            #NOT WORKING: 
            #AttributeError: ArangeData instance has no attribute 'readASCII'
            if data_format=='HDF5':
                readHDF5()
            elif data_format=='ASCII':
                readASCII()


            def readASCII():
                'doing stuff in here'

            def readHDF5():
                'doing other stuff in here, which is not the business of readASCII'

        def otherMemberFunction(self):
            'do not care what they are doing above!'

Upvotes: 0

Views: 253

Answers (1)

DeepSpace
DeepSpace

Reputation: 81604

  • You should move the definition of readASCII and readHDF5 so they are above the two if statements.
  • You don't need to have the dummy print statement in __init__. If you have nothing to initialize you can simply use pass, or better yet as @chepner commented don't even define __init__.

Upvotes: 2

Related Questions