5

I have a simple function, which I shall call myFunction. It takes two parameters, performs some calculations on them, and returns the result.

I also have a class, MyClass, which has a constructor that has a header like this:

__init__(self, bar, fun=myFunction):

When I try to run anything in this class, I get the following error:

MyClass
    def __init__(self, bar, fun=myFunction):
NameError: name 'myFunction' is not defined

If I remove this class, I can use myFun in the Python Shell, so what's the deal?

1
  • Please mark proper answer as a solution. We choose solutions that way, not by telling what is the solution by editing the question. Commented Jun 22, 2012 at 23:55

2 Answers 2

13

You haven't shown the actual code so it's hard to be sure, but I bet myFunction is defined after MyClass. The default value expression is evaluated when the __init__ method is defined, so myFunction must be defined at that point. Defining it later is too late.

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

12 Comments

Wow, you're right. All I did was cut and paste myFunction so that it is above MyClass in that file, and now everything is fine. Python is weird. Thanks.
+1 Exactly. To achieve similar effect, you can assign None (or anything else, but it must be defined) as default argument and later compare and eventually assign the myFunction function (this assignment will be evaluated when the method is actually called).
@user1123936: Python is one of the simplest (not meaning it is not powerful), easiest, most consistent programming languages I have ever known. Probably he is first in all of the above, so if you could mention something better, I would be very grateful :) Really.
@user1123936 the problem is that Python tries to read the file down and parses everything without executing until it can't anymore (because it needs to do some sort of calculation to parse the rest). I don't know the exact rationale behind it, but it makes intuitive sense as a tradeoff: you want to be able to dynamically execute and change your code on the fly without compiling, but you also want to be able to optimize it where possible.
@JeffTratner Python does execute as it reads. def is a statement. The def my_function(): ... block is executed when Python encounters it. The result of this execution is to create an object representing the code of the function, and assign it the name my_function.
|
-6

myFunction is a variable, not a value so you can't use it as a default parameter.

Maybe you could use a lambda function as the default parameter instead of the name of a declared function.

2 Comments

I did this exact same thing in another project though. MyClass and myFunction are actually both copied and pasted from there.
-1: Of course you are wrong (here: "_...is a variable, not a value so you can't use it as a default parameter..."). You can use anything you like as a default argument, but it (the assignment of this to the default argument) will be evaluated when the function (the one that has mentioned default argument) will be defined.

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.