0

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!'
1
  • Please read the Python Style Guide (PEP8). It's very good and will make your code much easier to read. You'll thank me later! Commented Aug 6, 2015 at 20:23

1 Answer 1

2
  • 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__.
Sign up to request clarification or add additional context in comments.

2 Comments

In general, it's probably better to just not define __init__ at all; this lets the parent class's __init__ be used if you have nothing to add.
Also since this is Python 2.7 OP is (probably inadvertently) using old-style classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.