Software engineering principles, from Robert C. Martin"s book Clean Code, adapted for Python. This is not a style guide. It"s a guide to producing readable, reusable, and refactorable software in Python.
Not every principle herein has to be strictly followed, and even fewer will be universally agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of Clean Code.
Inspired from clean-code-javascript
Targets Python3.7+
Bad:
importdatetimeymdstr=datetime.date.today().strftime("%y-%m-%d")Good:
importdatetimecurrent_date: str=datetime.date.today().strftime("%y-%m-%d")Bad: Here we use three different names for the same underlying entity:
defget_user_info(): passdefget_client_data(): passdefget_customer_record(): passGood: If the entity is the same, you should be consistent in referring to it in your functions:
defget_user_info(): passdefget_user_data(): passdefget_user_record(): passEven better Python is (also) an object oriented programming language. If it makes sense, package the functions together with the concrete implementation of the entity in your code, as instance attributes, property methods, or methods:
fromtypingimportUnion, Dict, TextclassRecord: passclassUser: info : str@propertydefdata(self) ->Dict[Text, Text]: return{} defget_record(self) ->Union[Record, None]: returnRecord() We will read more code than we will ever write. It"s important that the code we do write is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable.
Bad:
importtime# What is the number 86400 for again?time.sleep(86400)Good:
importtime# Declare them in the global namespace for the module.SECONDS_IN_A_DAY=60*60*24time.sleep(SECONDS_IN_A_DAY)Bad:
importreaddress="One Infinite Loop, Cupertino 95014"city_zip_code_regex=r"^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$"matches=re.match(city_zip_code_regex, address) ifmatches: print(f"{matches[1]}: {matches[2]}")Not bad:
It"s better, but we are still heavily dependent on regex.
importreaddress="One Infinite Loop, Cupertino 95014"city_zip_code_regex=r"^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$"matches=re.match(city_zip_code_regex, address) ifmatches: city, zip_code=matches.groups() print(f"{city}: {zip_code}")Good:
Decrease dependence on regex by naming subpatterns.
importreaddress="One Infinite Loop, Cupertino 95014"city_zip_code_regex=r"^[^,\\]+[,\\\s]+(?P<city>.+?)\s*(?P<zip_code>\d{5})?$"matches=re.match(city_zip_code_regex, address) ifmatches: print(f"{matches['city']}, {matches['zip_code']}")Don’t force the reader of your code to translate what the variable means. Explicit is better than implicit.
Bad:
seq= ("Austin", "New York", "San Francisco") foriteminseq: #do_stuff()#do_some_other_stuff()# Wait, what's `item` again?print(item)Good:
locations= ("Austin", "New York", "San Francisco") forlocationinlocations: #do_stuff()#do_some_other_stuff()# ...print(location)If your class/object name tells you something, don"t repeat that in your variable name.
Bad:
classCar: car_make: strcar_model: strcar_color: strGood:
classCar: make: strmodel: strcolor: strTricky
Why write:
importhashlibdefcreate_micro_brewery(name): name="Hipster Brew Co."ifnameisNoneelsenameslug=hashlib.sha1(name.encode()).hexdigest() # etc.... when you can specify a default argument instead? This also makes it clear that you are expecting a string as the argument.
Good:
fromtypingimportTextimporthashlibdefcreate_micro_brewery(name: Text="Hipster Brew Co."): slug=hashlib.sha1(name.encode()).hexdigest() # etc.Limiting the amount of function parameters is incredibly important because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument.
Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. Anything more than that should be consolidated. Usually, if you have more than two arguments then your function is trying to do too much. In cases where it"s not, most of the time a higher-level object will suffice as an argument.
Bad:
defcreate_menu(title, body, button_text, cancellable): passJava-esque:
classMenu: def__init__(self, config: dict): self.title=config["title"] self.body=config["body"] # ...menu=Menu({"title": "My Menu", "body": "Something about my menu", "button_text": "OK", "cancellable": False } )Also good
fromtypingimportTextclassMenuConfig: """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title: Textbody: Textbutton_text: Textcancellable: bool=Falsedefcreate_menu(config: MenuConfig) ->None: title=config.titlebody=config.body# ...config=MenuConfig() config.title="My delicious menu"config.body="A description of the various items on the menu"config.button_text="Order now!"# The instance attribute overrides the default class attribute.config.cancellable=Truecreate_menu(config)Fancy
fromtypingimportNamedTupleclassMenuConfig(NamedTuple): """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title: strbody: strbutton_text: strcancellable: bool=Falsedefcreate_menu(config: MenuConfig): title, body, button_text, cancellable=config# ...create_menu( MenuConfig( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!" ) )Even fancier
fromtypingimportTextfromdataclassesimportastuple, dataclass@dataclassclassMenuConfig: """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title: Textbody: Textbutton_text: Textcancellable: bool=Falsedefcreate_menu(config: MenuConfig): title, body, button_text, cancellable=astuple(config) # ...create_menu( MenuConfig( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!" ) )Even fancier, Python3.8+ only
fromtypingimportTypedDict, TextclassMenuConfig(TypedDict): """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title: Textbody: Textbutton_text: Textcancellable: booldefcreate_menu(config: MenuConfig): title=config["title"] # ...create_menu( # You need to supply all the parametersMenuConfig( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!", cancellable=True ) )This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, they can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you"ll be ahead of many developers.
Bad:
fromtypingimportListclassClient: active: booldefemail(client: Client) ->None: passdefemail_clients(clients: List[Client]) ->None: """Filter active clients and send them an email. """forclientinclients: ifclient.active: email(client)Good:
fromtypingimportListclassClient: active: booldefemail(client: Client) ->None: passdefget_active_clients(clients: List[Client]) ->List[Client]: """Filter active clients. """return [clientforclientinclientsifclient.active] defemail_clients(clients: List[Client]) ->None: """Send an email to a given list of clients. """forclientinget_active_clients(clients): email(client)Do you see an opportunity for using generators now?
Even better
fromtypingimportGenerator, IteratorclassClient: active: booldefemail(client: Client): passdefactive_clients(clients: Iterator[Client]) ->Generator[Client, None, None]: """Only active clients"""return (clientforclientinclientsifclient.active) defemail_client(clients: Iterator[Client]) ->None: """Send an email to a given list of clients. """forclientinactive_clients(clients): email(client)Bad:
classEmail: defhandle(self) ->None: passmessage=Email() # What is this supposed to do again?message.handle()Good:
classEmail: defsend(self) ->None: """Send this message"""message=Email() message.send()When you have more than one level of abstraction, your function is usually doing too much. Splitting up functions leads to reusability and easier testing.
Bad:
# type: ignoredefparse_better_js_alternative(code: str) ->None: regexes= [ # ... ] statements=code.split('\n') tokens= [] forregexinregexes: forstatementinstatements: passast= [] fortokenintokens: passfornodeinast: passGood:
fromtypingimportTuple, List, Text, DictREGEXES: Tuple= ( # ... ) defparse_better_js_alternative(code: Text) ->None: tokens: List=tokenize(code) syntax_tree: List=parse(tokens) fornodeinsyntax_tree: passdeftokenize(code: Text) ->List: statements=code.split() tokens: List[Dict] = [] forregexinREGEXES: forstatementinstatements: passreturntokensdefparse(tokens: List) ->List: syntax_tree: List[Dict] = [] fortokenintokens: passreturnsyntax_treeFlags tell your user that this function does more than one thing. Functions should do one thing. Split your functions if they are following different code paths based on a boolean.
Bad:
fromtypingimportTextfromtempfileimportgettempdirfrompathlibimportPathdefcreate_file(name: Text, temp: bool) ->None: iftemp: (Path(gettempdir()) /name).touch() else: Path(name).touch()Good:
fromtypingimportTextfromtempfileimportgettempdirfrompathlibimportPathdefcreate_file(name: Text) ->None: Path(name).touch() defcreate_temp_file(name: Text) ->None: (Path(gettempdir()) /name).touch()A function produces a side effect if it does anything other than take a value in and return another value or values. For example, a side effect could be writing to a file, modifying some global variable, or accidentally wiring all your money to a stranger.
Now, you do need to have side effects in a program on occasion - for example, like in the previous example, you might need to write to a file. In these cases, you should centralize and indicate where you are incorporating side effects. Don"t have several functions and classes that write to a particular file - rather, have one (and only one) service that does it.
The main point is to avoid common pitfalls like sharing state between objects without any structure, using mutable data types that can be written to by anything, or using an instance of a class, and not centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers.
Bad:
# type: ignore# This is a module-level name.# It"s good practice to define these as immutable values, such as a string.# However...fullname="Ryan McDermott"defsplit_into_first_and_last_name() ->None: # The use of the global keyword here is changing the meaning of the# the following line. This function is now mutating the module-level# state and introducing a side-effect!globalfullnamefullname=fullname.split() split_into_first_and_last_name() # MyPy will spot the problem, complaining about 'Incompatible types in # assignment: (expression has type "List[str]", variable has type "str")'print(fullname) # ["Ryan", "McDermott"]# OK. It worked the first time, but what will happen if we call the# function again?Good:
fromtypingimportList, AnyStrdefsplit_into_first_and_last_name(name: AnyStr) ->List[AnyStr]: returnname.split() fullname="Ryan McDermott"name, surname=split_into_first_and_last_name(fullname) print(name, surname) # => Ryan McDermottAlso good
fromtypingimportTextfromdataclassesimportdataclass@dataclassclassPerson: name: Text@propertydefname_as_first_and_last(self) ->list: returnself.name.split() # The reason why we create instances of classes is to manage state!person=Person("Ryan McDermott") print(person.name) # => "Ryan McDermott"print(person.name_as_first_and_last) # => ["Ryan", "McDermott"]Coming soon
We create a class called Burglar, which has a method called steal. This method breaks the SRP because it doesn't just steal. It also puts on and removes the invisibility cloak, which might lead to all sorts of issues for the burglar.
classBurglar: def__init__(self): self._artifacts= [] defsteal(self, artifact: str): print("Putting on the invisibility cloak.") print("Taking the artifact.") self._artifacts.append(artifact) print("Removing the invisibility cloak.") bilbo=Burglar() bilbo.steal("Arkenstone")A better way would be to create separate methods that can be called when appropriate.
classBurglar: def__init__(self): self._artifacts= [] defsteal(self, artifact: str): print("Taking the artifact.") self._artifacts.append(artifact) defcloak(self): print("Putting on the invisibility cloak.") defremove_cloak(self): print("Removing the invisibility cloak.") bilbo=Burglar() bilbo.cloak() bilbo.steal("Arkenstone") bilbo.remove_cloak() Now Bilbo can put on the cloak, walk in, steal the Arkenstone, walk out, so he won't be seen by Smaug and remove the cloak.
Example taken from:
https://codingwithjohan.com/blog/python/solid-single-responsibility-principle/
Software entities (classes, function, module) open for extension, but not for modification (or closed for modification)
The following example violated the OCP principle:
classDiscount: """Demo customer discount class"""def__init__(self, customer, price): self.customer=customerself.price=pricedefgive_discount(self): """A discount method"""ifself.customer=='normal': returnself.price*0.2elifself.customer=='vip': returnself.price*0.4This example is failed to pass the Open and Close Principle(OCP). Assume, we have a super VIP customer and we want to give a discount of 0.8 percentage. What would we do in this case? Maybe we will solve the problem this way.
....... defgive_discount(self): """A discount method"""ifself.customer=='normal': returnself.price*0.2elifself.customer=='vip': returnself.price*0.4elifself.customer=='supvip': returnself.price*0.8But this solution violates the OCP. Because we can’t modify the give_discount method. Only we can extend the method.
Solution:
classDiscount: """Demo customer discount class"""def__init__(self, customer, price): self.customer=customerself.price=pricedefget_discount(self): """A discount method"""returnself.price*0.2classVIPDiscount(Discount): """Demo VIP customer discount class"""defget_discount(self): """A discount method"""returnsuper().get_discount() *2classSuperVIPDiscount(VIPDiscount): """Demo super vip customer discount class"""defget_discount(self): """A discount method"""returnsuper().get_discount() *2Example from: https://medium.com/@vubon.roy/solid-principles-with-python-examples-10e1f3d91259
Let φ(x) be a property provable about objects x of type T. Then φ(y) should be true for objects y of type S where S is a subtype of T. More formally, this is the original definition (LISKOV 01) of Liskov’s substitution principle: if S is a subtype of T, then objects of type T may be replaced by objects of type S, without breaking the program.
Liskov Substitution Principle was introduced by Barbara Liskov in her conference keynote “Data Abstraction” in 1987.
Example of Violation of LSP
classVehicle: """A demo Vehicle class"""def__init__(self, name: str, speed: float): self.name=nameself.speed=speeddefget_name(self) ->str: """Get vehicle name"""returnf"The vehicle name {self.name}"defget_speed(self) ->str: """Get vehicle speed"""returnf"The vehicle speed {self.speed}"defengine(self): """A vehicle engine"""passdefstart_engine(self): """A vehicle engine start"""self.engine() classCar(Vehicle): """A demo Car Vehicle class"""defstart_engine(self): passclassBicycle(Vehicle): """A demo Bicycle Vehicle class"""defstart_engine(self): passIn Bicycle class violates the LSP. Cause in the Vehicle class has an engine method. But naturally, a bicycle has no engine. So we could not start any engine. Refactor the code and make a solution for this problem.
Solution:
classVehicle: """A demo Vehicle class"""def__init__(self, name: str, speed: float): self.name=nameself.speed=speeddefget_name(self) ->str: """Get vehicle name"""returnf"The vehicle name {self.name}"defget_speed(self) ->str: """Get vehicle speed"""returnf"The vehicle speed {self.speed}"classVehicleWithoutEngine(Vehicle): """A demo Vehicle without engine class"""defstart_moving(self): """Moving"""raiseNotImplementedclassVehicleWithEngine(Vehicle): """A demo Vehicle engine class"""defengine(self): """A vehicle engine"""passdefstart_engine(self): """A vehicle engine start"""self.engine() classCar(VehicleWithEngine): """A demo Car Vehicle class"""defstart_engine(self): passclassBicycle(VehicleWithoutEngine): """A demo Bicycle Vehicle class"""defstart_moving(self): passActually, LSP is a concept that applies to all kinds of polymorphism. Only if you don’t use polymorphism of all you don’t need to care about the LSP.
Example from: https://medium.com/@vubon.roy/solid-principles-with-python-examples-10e1f3d91259
Actually, This principle suggests that “A client should not be forced to implement an interface that it does not use”
Example of Violation of ISP:
classShape: """A demo shape class"""defdraw_circle(self): """Draw a circle"""raiseNotImplementeddefdraw_square(self): """ Draw a square"""raiseNotImplementedclassCircle(Shape): """A demo circle class"""defdraw_circle(self): """Draw a circle"""passdefdraw_square(self): """ Draw a square"""passIn the above example, we need to call an unnecessary method in the Circle class. Hence the example violated the Interface Segregation Principle. Solution:
classShape: """A demo shape class"""defdraw(self): """Draw a shape"""raiseNotImplementedclassCircle(Shape): """A demo circle class"""defdraw(self): """Draw a circle"""passclassSquare(Shape): """A demo square class"""defdraw(self): """Draw a square"""passAnother example:
classBankAccount: """A demo Bank Account class"""def__init__(self, balance: float, account: str): self.account={f"{account}": balance} defbalance(self, account: str): """Get current balance"""raiseNotImplementedclassDeposit(BankAccount): """A demo circle class"""defbalance(self, account: str): """Get current balance"""returnself.account.get(account) defdeposit(self, amount: float, account: str): """Deposit a new amount"""current=self.balance(account) new_amount=current+amountself.account.update({account: new_amount})Example from: https://medium.com/@vubon.roy/solid-principles-with-python-examples-10e1f3d91259
This principle suggests that below two points. a. High-level modules should not depend on low-level modules. Both should depend on abstractions. b. Abstractions should not depend on details. Details should depend on abstractions.
Example of Violation of DIP:
classBackendDeveloper: """This is a low-level module"""@staticmethoddefpython(): print("Writing Python code")classFrontendDeveloper: """This is a low-level module"""@staticmethoddefjavascript(): print("Writing JavaScript code")classProject: """This is a high-level module"""def__init__(self): self.backend=BackendDeveloper() self.frontend=FrontendDeveloper() defdevelop(self): self.backend.python() self.frontend.javascript() return"Develop codebase"project=Project() print(project.develop())Another example:
classNewsPerson: """This is a high-level module"""@staticmethoddefpublish(news: str) ->None: """ :param news: :return: """print(NewsPaper().publish(news=news))classNewsPaper: """This is a low-level module"""@staticmethoddefpublish(news: str) ->None: """ :param news: :return: """print(f"{news} Hello newspaper")person=NewsPerson() print(person.publish("News Paper"))The project class is a high-level module and backend & frontend are the low-level modules. In this example, we found that the high-level module depends on the low-level module. Hence this example are violated the Dependency Inversion Principle. Let’s solve the problem according to the definition of DIP.
Solution:
classBackendDeveloper: """This is a low-level module"""defdevelop(self): self.__python_code() @staticmethoddef__python_code(): print("Writing Python code") classFrontendDeveloper: """This is a low-level module"""defdevelop(self): self.__javascript() @staticmethoddef__javascript(): print("Writing JavaScript code") classDevelopers: """An Abstract module"""def__init__(self): self.backend=BackendDeveloper() self.frontend=FrontendDeveloper() defdevelop(self): self.backend.develop() self.frontend.develop() classProject: """This is a high-level module"""def__init__(self): self.__developers=Developers() defdevelops(self): returnself.__developers.develop() project=Project() print(project.develops())Second example:
classNewsPerson: """This is a high-level module"""@staticmethoddefpublish(news: str, publisher=None) ->None: print(publisher.publish(news=news)) classNewsPaper: """This is a low-level module"""@staticmethoddefpublish(news: str) ->None: print("{} news paper".format(news)) classFacebook: """This is a low-level module"""@staticmethoddefpublish(news: str) ->None: print(f"{news} - share this post on {news}") person=NewsPerson() person.publish("hello", NewsPaper()) person.publish("facebook", Facebook())Coming soon
For more information, please check the websites: