0

I am in the habit of using raw_input(...) for certain debugging. However, in python3 this has changed to input(...). Is there a way to define an alias at the top of my project, such as:

# __init__.py
raw_input = input

I tried the above, but it only worked in the file I added it to, and not any other files in that directory. I'd like this to work basically in every file within my python repository.

6
  • Do you mean, raw_input = input? Commented Oct 4, 2018 at 17:38
  • @Rishav yes, sorry. Thanks for pointing that out -- updated above. Commented Oct 4, 2018 at 17:39
  • Python 3's input method is already the equivalent of Python 2's raw_input. It's Python 2's input that doesn't exist anymore. Commented Oct 4, 2018 at 17:43
  • 1
    Nope. There's PYTHONSTARTUP, but that only works for interactive interpreter sessions. See also: How to add builtin functions (tldr: not really possible) Commented Oct 4, 2018 at 17:46
  • @PatrickHaugh got it -- so maybe I should just add that line at the top of each file I need to use it in? Commented Oct 4, 2018 at 17:47

3 Answers 3

3

You can define all aliases in a separate file (e.g. aliases.py) then import said file where needed (i.e. import aliases).

The con with this method that you'll be referencing the alias through aliases.alias unless you make the import stricter (i.e. from aliases import raw_input) or if you don't care about avoiding a wildcard import (i.e. from aliases import *).

Additionally, if you don't mind another import in the aliases file you can use the builtins namespace:

import builtins

builtins.raw_input = input

You still have to define all aliases separate file (e.g. aliases.py) then import said file where needed (i.e. import aliases) but the advantage of using the builtins namespace is that you can use that import exactly as given.

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

9 Comments

How is this different from just raw_input = input?
@Rishav This makes all the aliases available to all Python files easily with one import. Instead of having to redefine them in every file.
It just skirts around the real problem IMO; which is how to include a certain piece of functionality without explicitly importing it or adding it to the top of the file.
Does it? import aliases will only pull aliases into the current namespace, so now OP can write aliases.raw_input(...).
@Rishav A solution that looks like what OP seems to expect it to look like would involve modifying the Python parser. Which is almost universally considered a bad idea. You're basically forking the language at that point and creating your own personal dialect. Instead of doing that, you can put one import statement in each of your Python files.
|
0

You can do it by creating a module for creating the renaming function and then importing it to every file you want to like this:

First the module function declaration in alias.py

def raw_input(a):
    return input(a)

Secondly, import to another file:

from alias import raw_input
x = raw_input("hello world")
print(x)

Sadly, you will have to make the import of the module to every file you want to use the renamed function.

Hope it works for you!

3 Comments

How is this different from just raw_input = input?
sorry, forgot to clarify that the first part of the code is within another file called alias.py (already edited), the only difference I see is that he wanted to have the renamed function in every file he uses so it is available through the import. Fake Code Monkey Rashid answered the same way of solving it just a few seconds before me.
@Rishav is not wrong. You don't have to define raw_input as a new function since raw_input = input essentially makes them identical objects, and you can still import the same from aliases.
0

Put this at the top, and you will get exactly what you want.

import builtins
builtins.raw_input = builtins.input

It is guaranteed to work, but generally considered a bad practice (everybody will be confused with where is that raw_input defined)

3 Comments

This method does define an alias but it doesn't make said alias available across multiple files. I hope you don't mind but I elaborated on this method in my answer.
@FakeCodeMonkeyRashid this method needs to be executed once per python process. Then raw_input becomes available in every module of this process
I tested it and it does work as you described if the project is structured accordingly. If you are just working with a few files in an unstructured project then it is more likely to work as I described.

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.