0

Some python-2.7 and python-3.x functions have the same name, but perform differently. Can I import use a py2.7 function in python-3.x one, by changing its name?

The motivating example is python-2.7's "print", i.e. print "TEXT" which does not use without parenthesis, compared to print("TEXT") in python 3. Can I keep the python 2 "print" by binding it to something like pr?

(By the way, the issue for me is typing and escaping the brackets. The keys ( and ) are harder to press than a space bar. Also, because my IDE puts them in automatically, I then need to move my cursor out of it.)

Note: I asked this previously, yet has been wrongly marked as a duplicate. Again, to be clear, I'm specifically asking if I can bind a python 2 function to a new name in order to use its functionality in python-3.x.

Can I bind python-2.7's `print` in python-3.x, allowing me to use `print` without parenthesis in python-3.x?

I do not know how to contact moderators via internal message or correct this wrongful flag.

  • 2
    `print` in Python 2 is not a function, so even if you *could* somehow bind a Python 2 function and use it in Python 3, that wouldn't help. – kindall Jul 06 '17 at 00:07

2 Answers2

0

The real solution is to configure your IDE, rather than try and hack your way around those configuration problems.

That said, the Python 2 print statement with a space and no parenthesis does not exist in Python 3. While you can find ways to use the function from Python 2, the syntax cannot be used.

Software2
  • 2,358
  • 1
  • 18
  • 28
0

If your real problem is with the parentheses in print() for python 3, then no, as far as I know there's not really a solution. If it's a different function, you could always do

def funcName(arg):
    return anotherFunc(arg)

or as chepner comments

funcName = anotherFunc

effectively renaming anotherFunc().

As for your IDE specific issues, there's probably a way to turn off the automatic parentheses (or you could just use the arrow keys on your keyboard) and the more you use the parentheses, the faster you'll get using them, which is probably a good thing, as they're used in basically every function you'll ever use.

Finally, it's best not to force a language to do a specific thing that it doesn't really provide for. You don't use GOTOs in Python - they're not built in for a reason. You can write them using other methods. You use parentheses in python 3 - don't try to change that! There's another reason not to alter a language in the way you're describing - it decreases the readability of your code. Everyone will know what you mean when you write

''.join(something)

but not when you write

randomFuncName(something)

and then in some obscure spot you have a function like the one described above that renames ''.join.

Auden Young
  • 1,147
  • 2
  • 18
  • 39
  • You would have to write `def funcName(arg): return anotherFunc(arg)`, although it's much simpler (and more efficient) to just write `funcName = anotherFunc`, since functions are first-class objects. – chepner Jul 06 '17 at 00:38