Skip to content
Advertisement

How do python programmers deal with inability to use reserved keywords as names for variables in python (like`from`)? [closed]

I want to use name from for an argument of a function. But from is a reserved keyword, so python will throw an exception if I decide to use this name for a variable. How programmers in python deal with it? I think from is a common name for variables, in python as well. Is there any standards in python for how to rename the argument/variable in this case? Do they use prefixes/suffixes like from_?

Advertisement

Answer

Many languages have these challenges. Some ideas:

  • As mentioned in the comments, tack an underscore on the end: from_
  • Add context or type (also mentioned in comments): from => from_list
  • Mangle the spelling (from => frm, e.g. class => klass or cls)
  • Use a synonym: from => source

Each approach has its advantages and disadvantages.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement