Fred Yang
Fred Yang

Reputation: 2601

why this python chain function call fail in?

I have overridden a python class method like the following:

    def __dir__(self):

        # return  super().__dir__().append('extra') #failed
        # return  (super().__dir__()).append('extra') #failed

        #failed
        # rtn = super();
        # return rtn.__dir__().append('extra')

        #works
        rtn = super().__dir__()
        rtn.append('extra')
        return rtn

And get this error:

TypeError: 'NoneType' object is not iterable

I don't mind writing a few more lines of code, but I am just curious as to why the oneliner doesn't work?

Upvotes: 1

Views: 133

Answers (1)

Grismar
Grismar

Reputation: 31354

return  super().__dir__().append('extra')

Fails because append does not return the list, it returns None after modifying the list it gets.

rtn = super();
return rtn.__dir__().append('extra')

Fails because you effectively split pointing at the super() and then calling the method, but still has the same issue as the previous example.

You final example works, because you actually capture the list and then return it after modification.

Upvotes: 4

Related Questions