1

To change a function in python I know I can simply assign to a new variable, like:

imprimir = print
imprimir("test") # it prints "test" 

However I can't do that to import. Like import = importar. I would like to do the following:

importar = import
como = as

importar pandas como pd

I know that many people think I should do this in English, but I'm writing an introductory paper that absolutely needs to be in my native language.

Any tips on how to achieve this?

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
Artur Dutra
  • 515
  • 3
  • 18
  • 2
    No, import is a keyword. – Abdul Aziz Barkat Jan 23 '23 at 17:16
  • 2
    Does this answer your question? [Rename "def" in python (and other keywords)](https://stackoverflow.com/questions/36478009/rename-def-in-python-and-other-keywords), https://stackoverflow.com/questions/51992442/is-it-possible-to-redefine-keywords-in-python, https://stackoverflow.com/questions/17489603/defining-aliases-for-keywords-in-python – mkrieger1 Jan 23 '23 at 17:16
  • Thanks, I was also wondering what type was "import". Now I got it. Keywords are not be messed around – Artur Dutra Jan 23 '23 at 17:21
  • You'd better explain in your paper that this is a bad idea rather than translating default method/keywords – azro Jan 23 '23 at 17:25
  • You could write your own importer that translates the Portuguese keywords into the English keywords required by Python, but that's a fair bit of work, and then people would need the importer to run your code. And you'd still have to rename all the modules and methods... – kindall Jan 23 '23 at 17:32
  • I was wondering if it was possible. I know that is not advisable – Artur Dutra Jan 23 '23 at 17:33
  • 2
    Also, I don't speak Portuguese, but isn't that the infinitive form? you'd probably want to use the imperative form: "you, import!" rather than "to import." – kindall Jan 23 '23 at 17:35
  • That's a nice touch, @kindall... "importe pandas como pd" would be more appropriate if I were to use in portuguese – Artur Dutra Jan 23 '23 at 17:40
  • my high school Spanish classes prove their worth once again! – kindall Feb 09 '23 at 15:13

1 Answers1

2

import is a Keyword that is tokenized by the parser. Since this happens when parsing the .py source file, well before any code runs and any sort of assignment could happen, you are stuck.

Not completely. You could always grab the python source and change the parser. But, ouch. import is better.

There is an __import__ function. You could use that instead of import, but you'd still have to call it like a function, not use it as a keyword.

importar = __import__
pd = importar("pandas")

This would be confusing to most python programmers who see your code later. And would also confuse any code analysis tool such as an IDE offering type hints.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • That's very interesting, I didn't know it was possible – Artur Dutra Jan 23 '23 at 17:35
  • what about "if"? Could I translate it to "se" and that would even work? – Artur Dutra Jan 23 '23 at 17:36
  • Not completely, but you could write a function like `def se_nose(_truth_value, _if_true, _if_false)` Internally it would do `return _if_true() if _truth_value, else _if_false()` - now you have the right name, but the semantics are different. – tdelaney Jan 23 '23 at 19:37