[15 more] Signs of a professional Python programmer

Amit Singh Rathore
Dev Genius
Published in
3 min readMar 17, 2023

--

Following up on the previous blog

Click Here for Part I

  1. Using formatting with f — F string is a better & preferred way of string formatting over .format. This has support for variable substitution, expression evaluation, decimal rounding & precision. Note, F-String has one catch, the variable needs to be available in scope during declaration.
  2. Not treating file path as a string — Although file paths are strings manipulating them like a string is a little bit clumsy. For these seasoned programmers use pathlib module. pathlib.Path(“path/to/file.extension”)
  3. Using divmod — Many a time we do want to calculate quotient and remainder. In this, it is easier to use quotient, remainder = divmod(x, y) . Use of divmod, per se, is not that much significant rather knowing the right operator for right use case is.
  4. Not using single-letter variable — New python developer quickly write single letter variables although it saves clicks it is hard to understand the purpose and even harder to debug. Seasoned programmers prefer to use descriptive variable names.
  5. Using @property descriptor instead of getter setter — Using a descriptor is the more pythonic way of getting and setting attributes. @property @attribute.setter
  6. Using a copy of keys to iterable & store deletion key for the latter operation — While it is allowed to do deletion while looping over iterable, it may result in an error. If we wish to delete keys or items we can store them and then do a deletion separately.
  7. Using immutable data structure — It is very much intuitive to use a list, set & dictionary to never change parameters. As these data structures are mutable they have an overhead and are slow. Seasoned developers use structures like tuple, frozenset, MappingProxyType
  8. Using data class or named tuple for structured data— Seasoned python developers prefer to use data classes or NamedTuple for representing entities that contain data.
  9. Comparison with operator chaining — Operator chaining creates more readable and concise codeif a < b < c for if a <b and b < c
  10. Using "<separator>".join(Iterable) for string concatenation — New python developers quickly write string_variable += add_string. Although this works it is slow and has its own overhead. As a general guideline, if we are concatenating more than 3 strings, the developer use 'separator'.join(list_of_strings) .
  11. Using stringio for joining strings — Similar to the last point where we showed how people use join, many developers use string io buffer. string_buffer = io.StringIO() string_buffer.write() string_buffer.getvalue()
  12. Using specialized subclasses — Using a dictionary for everything is overkill. Seasoned developers prefer to use specific subclasses of data structure depending on what they want to achieve.from collections import defaultdict, Counter
  13. Using deepcopy —A deep copy creates a completely independent copy of the original object, unlike copy which just copies parent objects without recursive child traversal and copy. from copy import deepcopy
  14. Adding Progress Bar/Status — For long-running loops or processes showing a progress bar or logging progress status, this helps in letting the user know what’s going on.
  15. Using separate files for configuration (any of INI, TOML, YAML, ENV) — Having separate configuration/properties files helps in quick configuration. Otherwise, we will have to look at multiple places what properties are being set.

Thanks !!

--

--