def a(something):
return something*something
#Case I - referencing
b = a
#Case II - creating a new function to call the first
def b(something):
return a(something)
Which is better style? Are there drawbacks to either?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
b = ameans calls tobwill be faster (no overhead), but introspection (e.g.help(b)) will show the namea, anda‘s docstring. Unless the latter issues are a killer for your specific application (some kind of tutorial, for example), the speed advantage normally wins the case.Consider, e.g. in ref.py:
Now:
I.e., calls to
b1(pure referencing) are just as fast as calls toa(actually appear 2% faster in this run, but that’s well within “the noise” of measurement;-), calls tob2(entirely new function which internally callsa) incur a 20% overhead — not a killer, but normally something to avoid unless that performance sacrifice buys you something specific that’s quite important for your use case.