- Use the class name to get access to the class attributes:
Good example:
class Person:
city = "Kyiv"
def __init__(self, name: str):
self.name = name
@staticmethod
def print_city() -> None:
print(Person.city)
liz = Person("Liz")
liz.print_city() # Kyiv
Bad example:
class Person:
city = "Kyiv"
def __init__(self, name: str):
self.name = name
def print_city(self) -> None:
print(self.city)
liz = Person("Liz")
liz.print_city() # Kyiv
- You can change the
bool
value in one line:
Good example:
is_married = True
is_married = not is_married
Bad example:
is_married = True
if is_married:
is_married = False
else:
is_married = True
- Use the static method when needed.
- 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
- Make sure you use the double quotes everywhere.
Good example:
greetings = "Hi, mate!"
Bad example:
greetings = 'Hi, mate!'
- Use interpolation instead of concatenation:
Good example:
def print_full_name(name: str, surname: str) -> str:
return f"{{Name: {name}, surname: {surname}}}"
Bad example:
def print_full_name(name: str, surname: str) -> str:
return "{" + "Name:" + name + ", surname:" + surname + "}"
- Use descriptive and correct variable names:
Good example:
def get_full_name(first_name: str, last_name: str) -> str:
return f"{first_name} {last_name}"
Bad example:
def get_full_name(x: str, y: str) -> str:
return f"{x} {y}"
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.