Reputation: 13
I have a function and I want it to execute. Does anyone know how to do this?
def a():
a = 'print'
print a
Upvotes: 1
Views: 247
Reputation: 41665
To do this you just use.
a()
If you want this for use inside the current file use:
if __name__ == '__main__': a()
And if you want to pass the variable a on for use in other functions use:
if __name__ == '__main__': b = a()
The 'b' being the variable and 'a' being the function you have defined.
So the whole function could look like this:
def a():
b = 'print'
print b
if __name__ == '__main__': b = a()
Upvotes: 3
Reputation: 61529
The name of your function is a
. It takes no arguments. So call it using a()
:
>>> def a():
... a = 'print'
... print a
...
>>> a()
print
Note that you shadow the definition of a
as a function within a
itself, by defining a local variable with that same name. You may want to avoid that, as it may confuse you or other readers of your code. Also, it makes it impossible to apply recursion.
Anyway, in general, if f
is some function object then you can call it by putting parentheses behind it, possibly containing some arguments. Example:
>>> def twice(text):
... print text
... print text
...
>>> twice('the text I want to print twice')
the text I want to print twice
the text I want to print twice
Upvotes: 14
Reputation: 44226
I'm not sure I understood your question, but a simple function call goes as in
a()
of course, after the definition, so it would be
def a():
a = 'print'
print a
a()
Upvotes: 1