0

This code keeps giving me the error:

TypeError: 'NoneType' object has no attribute 'getitem'

class d_exposure(object):   
    def __init__(self):
        self.files = glob.glob('C:\files')
    def exposure(self,level):
        level = inspect.getargspec(d_exposure().exposure)[3][0]
        print level
def main():
    mp = d_exposure()
    mp.exposure(level = 'MID')

It seems that the problem is that it wants a default value for level. However, the traceback shows it is getting a value.

Traceback (most recent call last):
  File "C:\Users\Documents\my_scripts\exposure.py", line 58, in <module>
main()
  File "C:\Users\Documents\my_scripts\exposure.py", line 54, in main
mp.exposure(level = 'MID')
  File "C:\Users\Documents\my_scripts\exposure.py", line 17, in exposure
level = inspect.getargspec(d_exposure().exposure)[3][0]

When I try giving it a default value 'DIM', then the output has 'DIM', even though the call I made was mp.exposure(level = 'MID'). Can someone please help me figure out what I'm doing wrong?

3
  • 3
    This code doesn't make any sense. If you're passing in a value for level, just use it directly. There's no need for any introspection. Are you prehaps confusing the syntax for passing a keyword argument for the syntax declaring a default argument value? Commented Nov 3, 2016 at 23:14
  • Yes, this is obviously simplified from the whole code, but the idea is that I'm getting an error because it wants a default argument for level, but I want to be able to call the function for different types of 'levels' Commented Nov 3, 2016 at 23:24
  • 1
    I guess my point is: Why are you using that line of code? If you delete the entire line with inspect.getargspec, I suspect it would do what you want. If you want level to be an optional argument, you can use def exposure(self, level="DIM"):. Commented Nov 3, 2016 at 23:27

1 Answer 1

1
    inspect.getargspec(d_exposure().exposure)

This line gives you the following output:

ArgSpec(args=['self', 'level'], varargs=None, keywords=None,           defaults=None)

The last entry in that is None, on which you are trying to call the getitem method by accessing the 0th element. When you give default as 'DIM', the last value in the output of getargspec method is ['DIM'] and hence it gives you 'DIM' as the answer.

When you do this in the main method:

mp.exposure(level = 'MID')

you are giving MID as the parameter and not the default value. Default value can only be given during the function definition. You haven't stated clearly what exactly you want to do instead, so I can't give you input on that.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see I shouldn't be using [3][0] because that corresponds to default args, okay that makes sense. Thanks!

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.