In Python 2.x, names outside the local scope are wholly inaccessible except by passing them in, unless they happen to be in the global scope.
I have a class that has a couple of read-only/constant attributes that are used as parameters in a complex calculation of another class attribute. Instances of Foo should all have the same value of this calculated attribute. So I have something like this:
(Don't ask me why I need spam and eggs to slaughter a pig.)
In other words, if I have a complex attribute bacon whose calculation relies on helper functions and other attributes, I have to pass everything in. That isn't the end of the world, but this is a case where I would be tempted to use the nonlocal keyword in Python 3.x. (I'm using Python 2.7 here.)
I tried a few different ways of coding this and ended up somewhat confused. First, I thought this could also be done by making make_bacon a class method like so:
But apparently that doesn't work. It raise a NameError on the call to Foo.make_bacon(), saying Foo is not defined. (Also, doing bacon = make_bacon instead raises a TypeError, with the message "'classmethod' is not callable". I thought I knew why that doesn't work—no class is passed in—but the error message makes me doubt my understanding of what's going on there, too. ...)
Likewise, referencing Foo directly inside the helper methods, e.g.,
also raises a NameError. Which seems quite odd because Foo is indeed in the global scope and should be accessible. I have some examples sitting in front of me that do just this, but I get a NameError. Color me confused.
And of course I could just use the ordinary model of using instance attributes and calculate bacon each time an instance is initialized, but that's precisely what I want to avoid—calculating bacon is expensive, and anyway it should be the same for every instance.
Another alternative is to put all of this junk in the module's base scope and do the calculation there, but that also seems like an ugly solution.
Anyone care to elucidate what's going wrong in the classmethod and direct-Foo-call code? How would you code this?
Edit: rewritten to focus on the real issue
I have a class that has a couple of read-only/constant attributes that are used as parameters in a complex calculation of another class attribute. Instances of Foo should all have the same value of this calculated attribute. So I have something like this:
Code:
class Foo(object):
spam = 1
eggs = 2
def make_bacon(x, y, f):
dead_pig = f(x, y)
# do more stuff with dead_pig to make it into bacon
return bacon
def slaughter_the_pig(x, y)
# blah, blah
return dead_pig
bacon = make_bacon(spam, eggs, slaughter_the_pig)
In other words, if I have a complex attribute bacon whose calculation relies on helper functions and other attributes, I have to pass everything in. That isn't the end of the world, but this is a case where I would be tempted to use the nonlocal keyword in Python 3.x. (I'm using Python 2.7 here.)
I tried a few different ways of coding this and ended up somewhat confused. First, I thought this could also be done by making make_bacon a class method like so:
Code:
# inside Foo
@classmethod
def make_bacon(cls):
dead_pig = cls.slaughter_pig(cls.spam, cls.eggs)
# do more stuff with dead_pig to make it into bacon
return bacon
bacon = Foo.make_bacon()
Likewise, referencing Foo directly inside the helper methods, e.g.,
Code:
# inside Foo
def make_bacon():
dead_pig = Foo.slaughter_pig(Foo.spam, Foo.eggs)
# blah blah blah
return bacon
And of course I could just use the ordinary model of using instance attributes and calculate bacon each time an instance is initialized, but that's precisely what I want to avoid—calculating bacon is expensive, and anyway it should be the same for every instance.
Another alternative is to put all of this junk in the module's base scope and do the calculation there, but that also seems like an ugly solution.
Anyone care to elucidate what's going wrong in the classmethod and direct-Foo-call code? How would you code this?
Edit: rewritten to focus on the real issue