See Python PEP 8: Function and Variable Names:
Answer from S.Lott on Stack OverflowFunction names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
Is there a standard in naming your variables?
Python Class Inheritance: Adhering to Parent Class Naming Conventions vs. PEP 8 Compliance
Best practice for naming class instances?
Variable naming conventions
Videos
See Python PEP 8: Function and Variable Names:
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
The Google Python Style Guide has the following convention:
module_name,package_name,ClassName,method_name,ExceptionName,function_name,GLOBAL_CONSTANT_NAME,global_var_name,instance_var_name,function_parameter_name,local_var_name.
A similar naming scheme should be applied to a CLASS_CONSTANT_NAME
Hello!
How do you name your variables? Is there a standard?
I used to name my variables something like "firstNumber = 5" and constants like "MAX_SPEED = 200".
The problem I noticed is that when I look for those variables they are hard to spot on sight.
As such, I decided to write my next programs using notations like "number_of_items = 25".
I feel like using "_" is much more readable that using "numberOfItems = 25" that can look similar to "typesOfItems = 10"
I have a question regarding Python class inheritance and naming conventions. When I derive a class from another and want to implement functionalities similar to those in the parent class, should I reuse the same function names or adhere strictly to PEP 8 guidelines?
For example, I'm developing a class that inherits from QComboBox in PyQt6. I want to add a function to include a new item. In the parent class, addItem is a public function. However, I can't exactly override this function, so I've ended up with the following code:
def addItem(self, text, userData=None, source="program") -> None: # noqa: N802
"""
Add a single item to the combo box.
Set the item's text, user data, and checkable properties.
Depending on the data source, set it as (un)checked.
Item is checked if it has been added by user, unchecked otherwise.
"""
item = QStandardItem()
item.setText(text)
if userData is not None:
item.setData(userData)
item.setFlags(Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsUserCheckable)
# Set the check state based on the source
if source == "user":
print("Source is user")
item.setData(Qt.CheckState.Checked.value, Qt.ItemDataRole.CheckStateRole)
else:
print("Source is program")
item.setData(Qt.CheckState.Unchecked.value, Qt.ItemDataRole.CheckStateRole)
item.setData(source, Qt.ItemDataRole.UserRole + 1)
self.model().appendRow(item)
print(f"Added item: {text}, Source: {source}")
self.updateLineEditField()