conventions

Grouping Functions by Using Classes in Python

Grouping Functions by Using Classes in Python Question: I have been a Python Scientific Programmer for a few years now, and I find myself coming to a sort specific problem as my programs get larger and larger. I am self taught so I have never had any formal training and spent any time really on …

Total answers: 4

Python "private" function coding convention

Python "private" function coding convention Question: When writing a python module and functions in it, I have some “public” functions that are supposed to be exposed to outsiders, but some other “private” functions that are only supposed to be seen and used locally and internally. I understand in python there is no absolute private functions. …

Total answers: 1

Is there a standardized method to swap two variables in Python?

Is there a standardized method to swap two variables in Python? Question: In Python, I’ve seen two variable values swapped using this syntax: left, right = right, left Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped? …

Total answers: 8

Finding a list of all double-underscore variables?

Finding a list of all double-underscore variables? Question: Related: What is the common header format of Python files? Where can I find a list of all double-underscore variables that are commonly used in Python? In Python, variables starting and ending with double underscores are typically to store metadata or are built into the system. For …

Total answers: 3

When should a Python script be split into multiple files/modules?

When should a Python script be split into multiple files/modules? Question: In Java, this question is easy (if a little tedious) – every class requires its own file. So the number of .java files in a project is the number of classes (not counting anonymous/nested classes). In Python, though, I can define multiple classes in …

Total answers: 4

How do I add the contents of an iterable to a set?

How do I add the contents of an iterable to a set? Question: What is the “one […] obvious way” to add all items of an iterable to an existing set? Asked By: Ian Mackinnon || Source Answers: Use list comprehension. Short circuiting the creation of iterable using a list for example 🙂 >>> x …

Total answers: 6

Python __str__ versus __unicode__

Python __str__ versus __unicode__ Question: Is there a python convention for when you should implement __str__() versus __unicode__(). I’ve seen classes override __unicode__() more frequently than __str__() but it doesn’t appear to be consistent. Are there specific rules when it is better to implement one versus the other? Is it necessary/good practice to implement both? …

Total answers: 6

Python: using 4 spaces for indentation. Why?

Python: using 4 spaces for indentation. Why? Question: While coding python I’m using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it’s unusual. So, can anyone convince me to use 4 spaces instead of 2? What pros and cons? P.S. And finally, what’s easy way to …

Total answers: 13

Is it pythonic to import inside functions?

Is it pythonic to import inside functions? Question: PEP 8 says: Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. On occation, I violate PEP 8. Some times I import stuff inside functions. As a general rule, I do this if …

Total answers: 10