Python Forum
Creating a variable as a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a variable as a function
#1
In JavaScript there is an ability to make a variable a function.

Does that exists in python?

For example, could I do something like this?

Def x(y):
   print(y)

z= def x(‘hello’)

Z()
Thanks!
Reply
#2
You can't make anonymous functions, you have to give them a name. So in line 1, your function has the name x.

Once it has a name, you can assign it another name as well and call it that way.

In line 4, you're not just creating a function, you're trying to create a function with some private data (a closure). Yes, you can create such functions and return them.

def x(y):
    print(y)

def z():
    x("hello")

z()
new_name = z
new_name()
Output:
hello hello
Or a better example of a closure:

def make_printer(data):
    def printer_func():
        print(data)
    return printer_func

myhello = make_printer("Hello, world.")
myhello()
Output:
Hello, world.
Reply
#3
Anonymous Functions! It was driving me crazy trying to remember what they were called.

Thanks! That really makes things easier for me!
Reply
#4
I guess to say "can't" is a little bit strong. Python does have lambda expressions, but they can only be a single expression rather than everything that can be placed into a (named) function.

>>> squareit = lambda x: x**2 # no name, but assigned to squareit
>>> print(squareit(5))
25
But because of the limitations, I think of them as a separate category from python functions.
Reply
#5
The suggestion with lambda goes against PEP8 recommendations:

Quote:Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier:
# Correct:
def f(x):
    return 2*x

# Wrong:
f = lambda x: 2*x
the first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)

as to OP question - function is object as any other, you can always assign the function to variable:
def spam():
    print('This is spam')
eggs = spam
eggs()
This has plenty of useful applications, e.g. the pythonic way to create a menu/switch statement, using dict with functions as values.


also, you may be interested in functools.partial

from functools import partial
def raise_power(num, power):
    return num ** power

power_3 = partial(raise_power, power=3)
print(power_3(2))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 708 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 530 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  passing variable to function Rejoice 4 1,109 Sep-11-2020, 03:27 AM
Last Post: Pleiades
  [split] Creating a variable as a function DPaul 23 2,150 Sep-07-2020, 05:20 PM
Last Post: DPaul
  Function will not return variable that I think is defined Oldman45 6 1,258 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  Need help with for loop and variable value substitution in a function rsurathu 2 920 Jul-21-2020, 06:47 AM
Last Post: rsurathu
  Help creating a variable as a result Realen 2 714 Jun-18-2020, 04:46 AM
Last Post: Realen
  function error: undefined variable Atlantic_3000 3 2,219 Apr-12-2020, 02:53 AM
Last Post: bowlofred
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 806 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Creating function inside classes using type wolfmansbrother 3 880 Mar-20-2020, 01:21 AM
Last Post: wolfmansbrother

Forum Jump:

User Panel Messages

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