Reputation: 151
def func(raw_input()):
#stuff goes here..
Why can't you use raw_input() as a parameter?
Upvotes: 0
Views: 302
Reputation: 1198
Try this strange OOP solution.
class Runnable:
def __init__(self,function, *args, **kwargs):
self.function = function
self.args = args
self.kwargs = kwargs
def run(self):
return self.function(*self.args, **self.kwargs)
Runnable
Loads up an object with a function and arguments. .run()
executes when you're ready.
Here is its usage:
def func(runnable_thing):
print(runnable_thing.run())
super_raw = Runnable(raw_input, "Please Type your Input: ")
func(super_raw) # prints input from raw
The cool thing is that the Runnable class I made works on other functions with larger params as well.
import datetime
foo = Runnable(datetime.datetime, 2016, 12, 15 ) ## foo is not yet a datetime object
bar = foo.run() ## bar is a datetime object
If you're not a fan of the .run()
at the end of it, then we can follow Deloith's suggestion to use __call__()
to make Runnable feel like a function.
class Runnable:
def __init__(self,function, *args, **kwargs):
self.function = function
self.args = args
self.kwargs = kwargs
def __call__(self):
return self.function(*self.args, **self.kwargs)
From this point on, it really feels like we've created a new function.
def func(runnable_thing):
print(runnable_thing())
super_raw = Runnable(raw_input, "Please Type your Input: ")
func(super_raw) # prints input from raw
Try that on for size, and let me know how you like it.
Upvotes: 0
Reputation: 140148
You can't.
BTW, You could falsely think that using named parameters could work:
def func(s=raw_input()):
print(s)
func()
func()
then only problem is that it just seems to work. First time calling func()
, s
is evaluated by prompting you to enter a string.
Second time, default value for s
is known and you're not prompted.
Upvotes: 2
Reputation: 77827
Because the return value of a function call is semantically distinct from a parameter. raw_input() returns a string; a parameter must be an identifier that takes on the value passed in.
Upvotes: 3