0
def func(raw_input()):
    #stuff goes here..

Why can't you use raw_input() as a parameter?

4
  • Just what are you trying to accomplish? The code you posted is semantically inconsistent. Commented Dec 15, 2016 at 20:47
  • I'm not trying to accomplish anything. I was going to Zed's "Learn Python the hard way" and I thought about this when reading on functions and parameters. Commented Dec 15, 2016 at 20:48
  • 1
    what would you expect that code to do if it was valid? If it means "get raw input to use as an argument when this function is called" then how would you reference the string inputted? Commented Dec 15, 2016 at 20:51
  • Please explain what you would expect this code to do. What you posted is simply illegal syntax. Without knowing what you have in mind, the answer to "why can't you use this" is "because it's illegal", which isn't a particularly useful answer. Commented Dec 15, 2016 at 20:54

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

what about def func(s=raw_input): return s()?
that would work! but you may as well return raw_input(). I was just pointing out that you shouldn't force python to do stuff like that :)
Oh I agree it's silly to do either way :) and it would be preferable to bind raw_input() to a name within the function.
0

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.

1 Comment

I'd personally add on that you wouldn't necessarily want it to be a 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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.