Python Forum
is it considered good practice to ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is it considered good practice to ...
#1
is it considered good practice to return a reference to a function defined inside a function for the caller (or code it pass the reference along to) to call? would it matter for this if lambda was used?

note: this means the function that defined the function whose reference is returned, does return before that defined function gets called.

my coding intention is to eliminate a lot of tests in the function that are testing the same thing in each many calls with a choice definition that does only what is needed every time.  it does work, but i want to know how much flak i will get for doing it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(May-08-2017, 08:48 AM)Skaperen Wrote: is it considered good practice to return a reference to a function defined inside a function for the caller
This is perfectly fine and one of the wonderful things about functions as first class objects.
Quote:would it matter for this if lambda was used?
There is zero difference between a function defined with lambda and a standard function aside from where it can appear syntactically.
Reply
#3
(May-08-2017, 08:55 AM)Mekire Wrote:
Skaperen Wrote:would it matter for this if lambda was used?
There is zero difference between a function defined with lambda and a standard function aside from where it can appear syntactically.
i was wondering if using lambda would change how the practice is accepted by the community.  btdt both ways, so i know the interpreter accepts it.

or should i say compiler?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
There is no difference and unless it is a one liner I wouldn't use the lambda.

def power(exponent):
    def _internal(n):
        return n**exponent
    return _internal


def power_lam(exponent):
    return lambda n: n**exponent


cube = power(3)
cube_lam = power_lam(3)

print(cube(4))
print(cube_lam(4))
Reply
#5
(May-08-2017, 08:55 AM)Mekire Wrote: There is zero difference between a function defined with lambda and a standard function aside from where it can appear syntactically.

One other difference is that the __name__ attribute of a lambda is always '<lambda>', whereas the __name__ attribute of standard functions comes from the def statement. OTOH, I have never run into a situation where this mattered. And, of course, you can change that attribute.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
You can't use annotations with lambdas, if that matters to you.

>>> def outer():
...   def inner(n: int) -> int:
...     return n ** 2
...   return inner
...
>>> x = outer()
>>> x(4)
16
>>> def outer_lamb():
...   return lambda n: int -> int: n ** 2
 File "<stdin>", line 2
   return lambda n: int -> int: n ** 2
                         ^
SyntaxError: invalid syntax
Reply
#7
here is a snippet example i coded yesterday:

   if time_prefix:
        if sys.version_info.major < 3:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(now().strftime(etformat),*args,**opts)
                except (IOError,KeyboardInterrupt):
                    return
        else:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(now().strftime(etformat),*args,**opts)
                except (BrokenPipeError,IOError,KeyboardInterrupt):
                    return
    else:
        if sys.version_info.major < 3:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(*args,**opts)
                except (IOError,KeyboardInterrupt):
                    return
        else:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(*args,**opts)
                except (BrokenPipeError,IOError,KeyboardInterrupt):
                    return
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(May-09-2017, 12:36 AM)Skaperen Wrote: here is a snippet example i coded yesterday:

   if time_prefix:
        if sys.version_info.major < 3:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(now().strftime(etformat),*args,**opts)
                except (IOError,KeyboardInterrupt):
                    return
        else:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(now().strftime(etformat),*args,**opts)
                except (BrokenPipeError,IOError,KeyboardInterrupt):
                    return
    else:
        if sys.version_info.major < 3:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(*args,**opts)
                except (IOError,KeyboardInterrupt):
                    return
        else:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(*args,**opts)
                except (BrokenPipeError,IOError,KeyboardInterrupt):
                    return

I think I'd write that like:
printer = print
if time_prefix:
    printer = lambda *args, **opts: print(now().strftime(etformat),*args,**opts)
errors = (IOError, KeyboardInterrupt)
if sys.version_info.major >= 3:
    errors += (BrokenPipeError, )

def xprint(*args, **opts):
    opts["file"] = efile
    try:
        return printer(*args, **opts)
    except errors:
        pass
Reply
#9
Returning a function from inside a function is a decorator technique
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
nilamo: still, for the general case pattern, i will define functions.  in this case, i like what you did and will go back and see if i can put that in my code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is a good practice naming user defined python functions with prefix udf_? meerkat 2 297 Mar-09-2024, 01:40 PM
Last Post: buran
  best practice example data science mostafa705 1 718 Oct-13-2023, 04:44 AM
Last Post: KianLynch
  Practice coding through hackathon!! TheOutcast 0 1,422 Jun-11-2020, 12:03 PM
Last Post: TheOutcast
  Coding Practice BarakTu 2 3,248 Apr-14-2018, 05:31 AM
Last Post: buran
  Is it bad practice to return a variable and not use it? iFunKtion 8 6,414 Apr-04-2017, 04:34 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020