def func(raw_input()):
#stuff goes here..
Why can't you use raw_input() as a parameter?
def func(raw_input()):
#stuff goes here..
Why can't you use raw_input() as a parameter?
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.
def func(s=raw_input): return s()?return raw_input(). I was just pointing out that you shouldn't force python to do stuff like that :)raw_input() to a name within the function.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.
run() function that must be named to be used, but implement the magic hook __call__(), which lets an object pretend it's a function so you could call foo=Runnable(...); foo()