It is believed that the use of the word file as the name of the function argument is bylokodderstvo. Is it so? This is not a reserved Python word, so what could be the problem?

  • And where does such an opinion exist? - Avernial
  • In some circles of my communication. - faoxis

3 answers 3

The problem is that the use of variables with the same names as __buitins__ does not allow to call these __buitins__ in the same scope. For example, they often override type , id , and if further along the code it is necessary to use built-in functions of the same name, problems arise. And of course, this problem extends to the arguments of functions, which also extend to the entire scope of the function.

My opinion - the intersection of the field of visibility with __builtins__ permissible, but if possible, try to avoid. There are cases when you simply cannot avoid it - for example, when you define a database schema and you have a column called id . Do not get out. In Django instead of id preferable to use pk , for example, which by default refers to id .

  • one
    file not in __builtins__ . - Avernial
  • 6
    @Avernial In Python 2.7 file is in __builtins__ , and using this as a variable is highly undesirable - andreymal
  • Yes indeed. Already by default I consider that everything belongs to the 3rd python. - Avernial
  • @andreymal: Could you give an obvious reason why the file (namely the file name) is "extremely undesirable" to use. Where did you see that file used as __builtin__.file ? How often is such use justified? - jfs
  • @jfs i.imgur.com/9VndrsG.png - andreymal

In general, you should not use names that conflict with built-in ( dir(__builtins__) ); this can be misleading when reading code, but file is a special case : file not a built-in name in Python 3, and you should use open() in Python 2 open() instead of file() in most cases .

    If you look at the documentation for open in python 3 , then we see that the first argument is named as file . Of course, there is a possibility that among python developers there are "bydlokodery", but I think that there are no obstacles to using the word file in the argument.

    Help on built-in function open in module io:

    open (...)

    open (file, mode = 'r', buffering = -1, encoding = None, errors = None, newline = None, closefd = True, opener = None) -> file object

    • 3
      And in Python 2.7 this argument is called name;) - andmalmal