1

I have this function code

   def getList(self, cursor=self.conn_uat.cursor()):
        from pprint import pprint
        cursor.execute(...)

I am getting this error

NameError: name 'self' is not defined

The reason i am using in args is so that i dont put any dependency inside any function to make testing easier

2 Answers 2

3

self is only available inside the method getList. However, you are trying to access outside of the method in the function declaration line. Hence, you get a NameError.

I think the easiest/cleanest solution would be to do this instead:

def getList(self, cursor=None):
    from pprint import pprint
    cursor = self.conn_uat.cursor() if cursor is None else cursor
    cursor.execute(...)

The functionality of the above code is identical to what you were trying to use.

Also, here is a reference on conditional operators if you want it.

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

Comments

0

The function is defined in the class declaration, so the specific instance isn't known. The arguments are created at definition, not at run

Here's another example of it: "Least Astonishment" and the Mutable Default Argument

Comments

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.