- Prevent creating a redundant list via passing comprehension without square brackets when needed.
Good example:
def get_sum(numbers: list) -> int | float:
return sum(number for number in numbers)
Bad example:
def get_sum(numbers: list) -> int | float:
return sum([number for number in numbers])
- Do not repeat abstract methods from parent if you have an abstract class that inherits another abstract class.
Good example:
class Base(ABC):
@abstractmethod
def say_hi(self):
pass
class Derived(Base, ABC):
pass
class SayHi(Derived):
def say_hi(self):
print("Hi!")
Bad example:
class Base(ABC):
@abstractmethod
def say_hi(self):
pass
class Derived(Base, ABC):
@abstractmethod
def say_hi(self):
pass
class SayHi(Derived):
def say_hi(self):
print("Hi!")
- Use one style of quotes in your code. Double quotes are preferable.
- Use annotation, it is a good practice:
Good example:
def multiply_by_2(number: int) -> int:
return number * 2
Bad example:
def multiply_by_2(number):
return number * 2
- Use descriptive and correct variable names:
Good example:
players = ["Bob", "John"]
for player in players:
pass
Bad example:
players = ["Bob", "John"]
for i in players:
pass
Add comments, prints, and functions to check your solution when you write your code. Don't forget to delete them when you are ready to commit and push your code.