"Hungarian Notation" and type-checking

Is there any mileage in having a naming convention to indicate the type of a variable? I have never really liked the fact that the Python 'duck typing' policy is so lax, yet the new "Type Hints" package for Python 3 is rather clumsy, IMO.

For example:

github_response = requests.get('https://api.github.com/user', auth=('user', 'pass'))  # Derived from http://docs.python-requests.org/en/master.

The above request returns a Response object and so the variable has 'response' in its name.


word_count = total_words_in_file('text_file')

where 'count' has been defined (in the IDE, by the user perhaps) as an Integer and the function is known to return an Integer, perhaps via a local 'count' or 'total' variable.


I know that this has been attempted before but I think that an IDE like PyCharm could actually check variable usage and issue a warning if a conflict is detected. Also earlier usages of this 'Hungarian Notation' have largely been applied to compiled languages - rather strangely, in the case of known types - rather than an interpreted one like Python. 

Please note that I have shown suffixes above but prefixes could also be valid. I am not sure about relying on 'type strings' within a variable name however.

Is this idea feasible, do you think?

Thanks ...



1 comment
Comment actions Permalink

Hi Richard,

Surely, we considered this approach quite some time ago. You should admit, though, that such naming conventions aren't very reliable source of type information and they are applicable only to a limited number of cases. After all, we don't want to produce tons of false positives in the editor because of wrong types. However, we could use this information in more permissible features such as code completion, where it's fine to add extra items in the list of suggestions. In fact we do so already. Specifically, if we're not able to infer type of a variable by other means, we attempt to find classes with the same name (case insensitively) available in the containing file and then use them to suggest variable's attributes. Maybe, it's worth taking into account common prefixes/suffixes like "count", "total", "number", "num" etc. there as well. Anyway, as I said, it seems too risky to use the types determined this way for type checking. It's better to rely on PEP 484 type comments for this purpose.


Please sign in to leave a comment.