1

I'm trying to get user input as function call in a small python program but, when i'm using the input() method, python shell does not let me input anything and quit by itself.

from fileinput import input
def funcinput():
    comm = input("-->")
    print('your input is %s' %comm)

if __name__ == "__main__":
    print("try to get input")
    funcinput()

Code looks like this and every time I run it, python launcher just give me:

try to get input
your input is <fileinput.FileInput object at 0x10464c208>
>>> 

without let me input anything

What should I do to make it work?

2
  • There's something else you aren't showing in your actual code. It seems somewhere in your real code comm becomes a FileInput object. Commented Sep 22, 2016 at 2:43
  • yup you guys are all correct Commented Sep 22, 2016 at 2:56

1 Answer 1

2

The cause here is a masking of the built-in input. In some point, a from fileinput import input occurs, which masks builtins.input and replaces it with fileinput.input. That function doesn't return an str but, instead, returns an instance of FileInput:

>>> from fileinput import input

Using input now picks up fileinput.input:

>>> print(input("--> "))
<fileinput.FileInput at 0x7f16bd465160

If importing fileinput is necessary import the the module and access the input function using dot notation:

>>> import fileinput
>>> print(fileinput.input("--> ")) # fileinput input, returns FileInput instance.
>>> print(input("--> "))           # original input, returns str

If this option is failing (or, you have no control of imports for some reason), you could try and delete the imported reference to get the look-up for it to use the built-in version:

>>> from fileinput import input
>>> print(input("--> "))
<fileinput.FileInput at 0x7f16bd465f60>

Now by deleting the reference with del input:

>>> del input
>>> input("--> ")
--> 

input works as it always does.

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

1 Comment

Thank you so much! I've been stuck here for a day

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.