@@ -387,33 +387,42 @@ class FuncFormatter(Formatter):
387
387
def __init__ (self , func ):
388
388
self .func = func
389
389
390
+ def __call__ (self , x , pos = None ):
391
+ """
392
+ Return the value of the user defined function.
393
+
394
+ *x* and *pos* are passed through as-is.
395
+ """
396
+ if self ._nargs == 1 :
397
+ return self ._func (x )
398
+ return self ._func (x , pos )
399
+
400
+ @property
401
+ def func (self ):
402
+ return self ._func
403
+
404
+ @func .setter
405
+ def func (self , func ):
406
+ self ._func = func
390
407
if not isinstance (func , types .BuiltinFunctionType ):
391
- self .nargs = len (inspect .signature (func ).parameters )
408
+ self ._nargs = len (inspect .signature (func ).parameters )
392
409
elif (isinstance (getattr (func , "__self__" ), str ) and
393
410
(getattr (func , "__name__" , "" ) == "format" )):
394
411
#check if there's a format spec
395
- self .nargs = len ([(_ , _ , fs , _ ) for (_ , _ , fs , _ )
412
+ self ._nargs = len ([(_ , _ , fs , _ ) for (_ , _ , fs , _ )
396
413
in string .Formatter ().parse (func .__self__ )
397
414
if fs is not None ])
398
415
else :
399
416
#finding argcount for other builtins is a mess
400
- self .nargs = 2
417
+ self ._nargs = 2
401
418
cbook ._warn_external (f"{ func .__name__ } is not supported "
402
419
"and may not work as expected" )
403
- if self .nargs not in [1 , 2 ]:
404
- raise TypeError (f"{ func .__name__ } takes { self .nargs } arguments. "
420
+ if self ._nargs not in [1 , 2 ]:
421
+ raise TypeError (f"{ func .__name__ } takes { self ._nargs } arguments. "
405
422
"FuncFormatter functions take at most 2: "
406
423
"x (required), pos (optional)." )
424
+
407
425
408
- def __call__ (self , x , pos = None ):
409
- """
410
- Return the value of the user defined function.
411
-
412
- *x* and *pos* are passed through as-is.
413
- """
414
- if self .nargs == 1 :
415
- return self .func (x )
416
- return self .func (x , pos )
417
426
418
427
419
428
class FormatStrFormatter (Formatter ):
0 commit comments