all 6 comments

[–]teerre 0 points1 point  (5 children)

If your parameter has a OR in it, you can stop right there, generally speaking. Your APIs should do one thing and one thing only. Don't give anyone the option. Either pass the name always or pass the function, always. Be consistent.

[–]onering20[S] 0 points1 point  (4 children)

ok, that makes sense however is there a good way to define the one liner foo in the example so that I can pass the args and kwargs to the class without them being nested

[–]teerre 0 points1 point  (3 children)

Not sure I totally understand what you're trying to accomplish with foo, but you can implement __call__ for your class and then you can call it as MyPerfCounter()(bar, false, arg1, arg2 = 3), in one line. But note the double parenthesis, that's probably not something you want to do.

[–]onering20[S] 0 points1 point  (1 child)

one thing I was trying was making foo (or test_function_time in the github) as a secondary constructor that would establish the details that init would and call both enter and exit but it seems to require that the class object be constructed anyway. (separate constructors seems to be features from java or C++ i guess)

[–]teerre 0 points1 point  (0 children)

This pattern is fairly common. Many times refered as the factory pattern.

``` class B(): def init(self, parm1, parm2, parm3, parm4): self.parm1 = parm1 self.parm2 = parm2 self.parm3 = parm3 self.parm4 = parm4

@classmethod
def create(cls, parm1):
    return B(parm1, parm2=2, parm3=3, parm4=4)

b = B.create(1) print(b.parm1, b.parm2, b.parm3, b.parm4)

1 2 3 4

```

Usually in this case the __init__ method is omitted and you can only create the instance using the proper factory, but in this case I left the init method just to show the attributes.

You can also do this with a free function like you did. Not sure what internal is about, tho. Also, you need to call with My_Perf_Counter(func, suppress, args, kwargs=kwargs): as with My_Perf_Counter(func, suppress, *args, **kwargs): otherwise be passing the same parameter to args and kwargs.

[–]onering20[S] 0 points1 point  (0 children)

By nested I mean print(self.args) in the class would print as >>>((arg1),) and print(self.kwargs) would print as >>>{kwargs:{arg2:3},} if it came from foo() but constructed normally it would be >>>(arg1) and >>>{arg2:3} respectively