I\'m trying to rewrite some code using classes. At some point what I want is assign a member function a particular definition using a parameter value for each instance of an
self?In Python, every normal method is forced to accept a parameter commonly named self. This is an instance of class - an object. This is how Python methods interact with a classes state.
You are allowed to rename this parameter whatever you please. but it will always have the same value:
>>> class Class:
def method(foo): #
print(foo)
>>> cls = Class()
>>> cls.method()
<__main__.F object at 0x03E41D90>
>>>
However, what you are probably confused about is how this code works differently:
>>> class Class:
def method(foo):
print(foo)
methods = {'method': method}
def __init__(self):
self.run = self.methods['method']
>>> cls = Class()
>>> cls.run(3)
3
>>>
This is because of the distinction between bound, and unbound methods in Python.
When we do this in __init__():
self.run = self.methods['method']
We are referring to the unbound method method. That means that our reference to method is not bound to any specific instance of Class, and thus, Python will not force method to accept an object instance. because it does not have one to give.
The above code would be the same as doing this:
>>> class Class:
def method(foo):
print(foo)
>>> Class.method(3)
3
>>>
In both examples, we are calling the method method of the class object Class , and not an instance of the Class object.
We can further see this distinction by examining the repr for a bound and unbound method:
>>> class Class:
def method(foo):
print(foo)
>>> Class.method
>>> cls = Class()
>>> cls.method
>
>>>
As you can see, in the first example when we do Class.method, Python shows:
. I've lied to you a little bit. When we have an unbound method of a class, Python treats them as plain functions. So method is simply a function that is not bound to any instance of `Class.
However in the second example, when we create an instance of Class, and then access the method object of it, we see printed: .
The key part to notice is bound method Class.method. That means method is bound to cls - a specfic an instance of Class.
As @jonshapre mentioned, writing code like in your example leads to confusion (as proof by this question), and bugs. It would be a better idea if you simply defined nonLinearBipolarStep() outside of Activation, and reference that from inside of Activation.activation_functions:
def nonLinearBipolarStep(self,x,string=None):
if not string: return (-1 if x<0 else 1 )
else: return ('-' if x<0 else '1')
class Activation:
activation_functions = {
'bipolar': nonLinearBipolarStep,
}
...
I guess a more specific question would be: what should I pay attention to on that code in order to become evident that
ag.run(x)would be a call to an unbound function?
If you'd still like to let nonLinearBipolarStep be unbound, then I recommend simply being carefully. If you think your method would make for the cleanest code then go for it, but make sure you know what you are doing and the behavior your code will have.
If you still wanted to make is clear to users of your class that ag.run() would be static, you could document it in a docstring somewhere, but that is something the user really shouldn't even have to be concerned with at all.