Apparently Inconsistent Errors When Calling Functions Defined In Other Modules From Within A Class
Solution 1:
Because seasonal_decompose
is a function
instance and numpy.sin
is a ufunc
instance. Both are callable and are used pretty much as functions, but only seasonal_decompose
is treated as a bona fide Python function. And Python functions are treated special.
The <class 'method'>
you're printing out should not be confused with class methods but that its class is method
, more specifically a bound method. Bound methods are how Python wraps function
instances to instance or class methods. Maybe a simple example will help:
defouterf(): pass# a true Python functionclassFoo:
f = outerf
defg(self): passprint(f) # <function f at ...>print(Foo.f) # <function outerf at ...>print(Foo.g) # <function Foo.g at ...>print()
x = Foo()
print(x.f) # <bound method outerf of <__main__.Foo object at ...>>print(x.g) # <bound method Foo.g of <__main__.Foo object at ...>>
See how all of these start out as function
instances in the class Foo
but end up as bound methods in the Foo
instance? When an instance is made, Python checks its class attributes for function
instances and makes bound methods, no matter if the function
was defined in the class or not.
So why does Python make bound methods? To implement instance method call syntax where the instance is automatically passed to the wrapped function's first argument.
f()
Foo.f()
Foo.g(x)
x.f() # TypeError: outerf() takes 0 positional arguments but 1 was given
x.g() # same as Foo.g(x)
Well okay, this is convenient and all, but what if you want to opt out? You want your function to stay a function in its instance and its class. Well that's what static methods are for. Now, you're not going to use the @staticmethod
decorator here because decorators only work on function or class definitions, and your function is defined in another module. But you have the staticmethod
builtin:
from statsmodels.tsa.seasonal import seasonal_decompose
classFoo4:
my_seasonal_decompose = staticmethod(seasonal_decompose)
print(seasonal_decompose) # <function seasonal_decompose at ...>print(Foo4.my_seasonal_decompose) # <function seasonal_decompose at ...>
x4 = Foo4()
print(x4.my_seasonal_decompose) # <function seasonal_decompose at ...>
Post a Comment for "Apparently Inconsistent Errors When Calling Functions Defined In Other Modules From Within A Class"