class Foo(object):
def run(self, context): ...
Objects with only a single method to run it are already built into Python, they are the functions. def foo(context): ... class Foo(object):
def run(self, context): ...
Objects with only a single method to run it are already built into Python, they are the functions. def foo(context): ... def foo(context):
def bar(x):
return x*2
baz = bar(context)
... This is particularly idiomatic for higher order functions, but I think it can be useful for other things if used with restraint.But if you have functions that only need to be called from one calling function then I why not just inline the code and eschew having a function at all. Long function bodies for the win!
When you write the initial code to figure out bar, just throw that code in a unit test so you can run it any time. Stop throwing tests away!
The nested function is, I think, fine when it’s so tiny it’s not worth unit testing (like, it-could-have-been-a-lambda small) or when bar is returned by foo (ie foo is higher order), in which case you can test the return value. Apart from that… restraint!