Here I follow the idea from the answer by @argentum2f to copy the attributes. This can be automated by a decorator and it works with Python 3. Of course copying the attributes means that they cannot be changed, hence the name @const_self for the decorator.
With @const_self you define a method with first arguments that have the same names as the attributes you want to use — and no self.
from cmath import sqrt
def const_self(fun):
fun_args = fun.__code__.co_varnames[:fun.__code__.co_argcount]
def fun_with_self(*args, **kwargs):
self = args[0]
other_args = list(args[1:])
used_attributes = [arg for arg in fun_args if hasattr(self, arg)]
self_args = [getattr(self, attr) for attr in used_attributes]
return fun(*(self_args + other_args), **kwargs)
return fun_with_self
class QuadraticEquation:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
@const_self
def roots(a, b, c, dummy, lazy = False):
print("Dummy is", dummy)
if lazy: return # A lazy calculator does not calculate
return (-b - sqrt(b**2 - 4*a*c)) /2/a, (-b + sqrt(b**2 - 4*a*c)) /2/a
Of course a lot should be improved in that code: At least it fails if you define a method like def rootsfun(a, dummy, b, c): print(a,b,c) here and it does not preserve the docstring. But I think it demonstrates the idea well enough.
Here I follow the idea from the answer by @argentum2f to copy the attributes. This can be automated by a decorator and it works with Python 3. Of course copying the attributes means that they cannot be changed, hence the name @const_self for the decorator.
With @const_self you define a method with first arguments that have the same names as the attributes you want to use — and no self.
from cmath import sqrt
def const_self(fun):
fun_args = fun.__code__.co_varnames[:fun.__code__.co_argcount]
def fun_with_self(*args, **kwargs):
self = args[0]
other_args = list(args[1:])
used_attributes = [arg for arg in fun_args if hasattr(self, arg)]
self_args = [getattr(self, attr) for attr in used_attributes]
return fun(*(self_args + other_args), **kwargs)
return fun_with_self
class QuadraticEquation:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
@const_self
def roots(a, b, c, dummy, lazy = False):
print("Dummy is", dummy)
if lazy: return # A lazy calculator does not calculate
return (-b - sqrt(b**2 - 4*a*c)) /2/a, (-b + sqrt(b**2 - 4*a*c)) /2/a
Of course a lot should be improved in that code: At least it fails if you define a method like def roots(a, dummy, b, c): print(a,b,c) here and it does not preserve the docstring. But I think it demonstrates the idea well enough.
Here I follow the idea from the answer by @argentum2f to copy the attributes. This can be automated by a decorator and it works with Python 3. Of course copying the attributes means that they cannot be changed, hence the name @const_self for the decorator.
With @const_self you define a method with first arguments that have the same names as the attributes you want to use — and no self.
from cmath import sqrt
def const_self(fun):
fun_args = fun.__code__.co_varnames[:fun.__code__.co_argcount]
def fun_with_self(*args, **kwargs):
self = args[0]
other_args = list(args[1:])
used_attributes = [arg for arg in fun_args if hasattr(self, arg)]
self_args = [getattr(self, attr) for attr in used_attributes]
return fun(*(self_args + other_args), **kwargs)
return fun_with_self
class QuadraticEquation:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
@const_self
def roots(a, b, c, dummy, lazy = False):
print("Dummy is", dummy)
if lazy: return # A lazy calculator does not calculate
return (-b - sqrt(b**2 - 4*a*c)) /2/a, (-b + sqrt(b**2 - 4*a*c)) /2/a
Of course a lot should be improved in that code: At least it fails if you define a method like def fun(a, dummy, b, c): print(a,b,c) here and it does not preserve the docstring. But I think it demonstrates the idea well enough.
Here I follow the idea from the answer by @argentum2f to copy the attributes. This can be automated by a decorator and it works with Python 3. Of course copying the attributes means that they cannot be changed, hence the name @const_self for the decorator.
With @const_self you define a method with first arguments that have the same names as the attributes you want to use — and no self.
from cmath import sqrt
def const_self(fun):
fun_args = fun.__code__.co_varnames[:fun.__code__.co_argcount]
def fun_with_self(*args, **kwargs):
self = args[0]
other_args = list(args[1:])
used_attributes = [arg for arg in fun_args if hasattr(self, arg)]
self_args = [getattr(self, attr) for attr in used_attributes]
return fun(*(self_args + other_args), **kwargs)
return fun_with_self
class QuadraticEquation:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
@const_self
def roots(a, b, c, dummy, lazy = False):
print("Dummy is", dummy)
if lazy: return # A lazy calculator does not calculate
return (-b - sqrt(b**2 - 4*a*c)) /2/a, (-b + sqrt(b**2 - 4*a*c)) /2/a
Of course a lot should be improved in that code: At least it fails if you define a method like def roots(a, dummy, b, c): print(a,b,c) here and it does not preserve the docstring. But I think it demonstrates the idea well enough.