Let's say I have a class like this:
class Foo(namedtuple('Foo', ['f1'])):
def f1(self):
print('Default f1')
def __new__(cls, f1=f1):
return super().__new__(cls, f1)
And let's say I create a Foo object later on and choose to override the method definition for f1, like so:
def my_f1():
print("My f1")
foo = Foo(f1=my_f1)
If I then try:
foo.f1()
I get:
Default f1
I'm trying to get "My f1" to print, of course. I also want f1 to be optional. What's going on here? Is it possible to define a default implementation for a method in a namedtuple and then override it in new?