M
Michael Rudolf
Out of curiosity I tried this and it actually worked as expected:
x=[]
foo=x.append
def f(self):
return self.x
At first I thought "hehe, always fun to play around with python. Might
be useful sometimes" - but then It really confused me what I did. I
mean: f is what we call a method, right? But was is foo? It is not a
method and not a classmethod as it accepts no self and no cls.
So that leaves staticmethod? OK, fair, as x is "static" here anyway this
reflects what it does. But then consider this:
def __init__(self):
self.x=[]
self.foo=self.x.append
def f(self):
return self.x
Note that all I did was moving the list and foo into the instance. Still
no self and no cls, but also no static behaviour any more.
So is foo just nothing of the above and really only a class/instance
attribute which happens to be callable?
Perhaps this all does not matter, but now I am really confused about the
terminology. So: what makes a method a method? And of what type?
Regards,
Michael
x=[]
foo=x.append
def f(self):
return self.x
>>> t=T()
>>> t.f() []
>>> T.foo(1)
>>> t.f() [1]
>>>
At first I thought "hehe, always fun to play around with python. Might
be useful sometimes" - but then It really confused me what I did. I
mean: f is what we call a method, right? But was is foo? It is not a
method and not a classmethod as it accepts no self and no cls.
So that leaves staticmethod? OK, fair, as x is "static" here anyway this
reflects what it does. But then consider this:
def __init__(self):
self.x=[]
self.foo=self.x.append
def f(self):
return self.x
>>> y=T()
>>> y.x []
>>> y.foo(1)
>>> y.x [1]
>>> a=T()
>>> a.x []
>>> a.foo(2)
>>> a.x [2]
>>>
Note that all I did was moving the list and foo into the instance. Still
no self and no cls, but also no static behaviour any more.
So is foo just nothing of the above and really only a class/instance
attribute which happens to be callable?
Perhaps this all does not matter, but now I am really confused about the
terminology. So: what makes a method a method? And of what type?
Regards,
Michael