coding-style

How can I clean this code ? (rock paper scissors)

How can I clean this code ? (rock paper scissors) Question: I made rock paper scissors game with score counter. Although it works well, I realized that the code is too heavy. import random game = 3 userScore = 0 computerScore = 0 while game != 0: print(f"game left : {game}") user = input("’r’ for …

Total answers: 2

Python: variable naming convention – file, path, filepath, file_path

Python: variable naming convention – file, path, filepath, file_path Question: I would like to know the correct naming convention for the following variables in Python which I couldn’t find one from Google Style Guide and PEP8 (Let’s say I have the following Python code) output_file = open(output_file_path, ‘w’) What would be the best variable name …

Total answers: 2

How to name variable correctly to avoid warning like "Shadows name from outer scope" in Python

How to name variable correctly to avoid warning like "Shadows name from outer scope" in Python Question: I use PyCharm for my python program, and I wrote codes below: def get_files_name(): root_dir = “/Volumes/NO NAME/” for root, ds, fs in os.walk(root_dir): for f in fs: print(os.path.join(root_dir, f)) get_files_name() for root, ds, fs in os.walk(other_dir): pass …

Total answers: 4

What does it means to not exceeding 80 characters in a line in Python?

What does it means to not exceeding 80 characters in a line in Python? Question: There is a mention that it is preferable for that a line in a Python script should not exceed more than 80 characters. For example: print(“A very long strings”) Does it include the characters of the print function, the strings …

Total answers: 4

What is a common way to return the result of method call in Python class?

What is a common way to return the result of method call in Python class? Question: I have a class in Python which trains a model for the given data: class Model(object): def __init__(self, data): self.data = data self.result = None def train(self): … some codes for training the model … self.result = … Once …

Total answers: 2

Multi-line description of a parameter description in python docstring

Multi-line description of a parameter description in python docstring Question: So, reStructuredText is the recommended way for Python code documentation, if you try hard enough, you can find in the sphinx documentation how to normalize your function signature documentation. All given examples are single-line, but what if a parameter description is multi-line like the following …

Total answers: 5

Grouping constants in python

Grouping constants in python Question: This is mainly a “good python style” question. I have a module which is using a number of constants that feels should be grouped. Lets say we have Dogs and cat and each of them have number of legs and favorite food. Note that we want to model nothing but …

Total answers: 5

coding style: lightweight/simplest way to create instances supporting attribute assignment?

coding style: lightweight/simplest way to create instances supporting attribute assignment? Question: I often have to create a result object instance to return complex values from functions and wonder what’s a nice Pythonic approach. I am aware of the closely related attribute-assignment-to-built-in-object, but it is asking why he needs to hack a subclass workaround to use …

Total answers: 3

Python coding convention "Wrong continued indentation before block: found by pylint

Python coding convention "Wrong continued indentation before block: found by pylint Question: I used pylint to check my python code, and found this convention problem: C:11, 0: Wrong continued indentation before block. + this_time <= self.max): ^ | (bad-continuation) I tried to refine for times but the problem is still present, can someone help? Thanks! …

Total answers: 4

proper formatting of python multiline [ for in ] statement

proper formatting of python multiline [ for in ] statement Question: How should i format a long for in statement in python ? for param_one, param_two, param_three, param_four, param_five in get_params(some_stuff_here, and_another stuff): I have found that i can brake a for in statement only with a backslash : for param_one, param_two, param_three, param_four, param_five …

Total answers: 2