I have a class that will have a number of external methods that will all call the same smaller set of internal methods. So something like:
obj.method_one(a, c) and
obj.method_two(a, c)
where obj.method_one calls obj._internal_method(a, c, y) and obj.method_two calls obj._internal_method(a, c, z).
They're nearly identical but they have one argument at the end that differs on the internal call (which is a constant).
I could just copy and paste a few times but I was wondering if theres any way to dynamically create these external methods.
Best Answer
There are ways, but unless you have at least half a dozen, I'd suggest:
The other option is to actually really create it dynamically. This can be confusing, because if the object is inspected, the methods aren't there. It looks like this:
This makes use of the fact that a getting a property (like a method) that does not exist, the object's
__getattr__
is called, and the result is passed back. Note that we are passing back the FUNCTION_internal_method
asa.method_one
, not calling it at this stage; that is done after the call to getattr has returned. Think of the last line as being:The first of these calls
__getattr__
which returns_internal_method
(but does not call it), the latter calls_internal_method
.Since _internal_method is declared within
__getattr__
, it has the same scope as the call that created it, soself
andattrname
are accessible. Also,_internal_method
is TRULY private, not just "private by convention".I will reiterate that this is confusing for the next person to maintain your code. For example, if you ask for
help(a.method_one)
, you will get the help for_internal_method
instead!The length of this explanation, and your probable confusing is a very good reason not to use this unless you have to.