You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Do's * Always prefer to use 1st syntax - **Absolute Imports**
743
746
* Put all imports in a central module: In __init__.py
747
+
744
748
```python
745
749
from . import a
746
750
from . import b
747
751
```
748
752
749
753
It has two major flaws:\\
750
754
it forces all the submodules to be imported, even if you're only using one or two, and you still can't look at any of the submodules and quickly see their dependencies at the top, you have to go sifting through functions.
755
+
751
756
### Don'ts * you shouldn't be using the 4th syntax, since it only works in python2 and runs the risk of clashing with other 3rd party modules.
752
757
753
758
## Classes
759
+
754
760
When defining a class who does not inherit give it ''object'' instead of nothing.
755
761
756
762
Not every method should be accessed outside of the class.
@@ -788,11 +794,13 @@ def add(a,b):
788
794
## Functions/Methods
789
795
790
796
### Closures
797
+
791
798
User-defined functions incur a layer of overhead that hurts performance.
792
799
793
800
Therefore, lambdas and nested functions should be avoided (unless it promotes readability).
794
801
795
802
### Recursion
803
+
796
804
Python does not optimize for recursion.
797
805
798
806
The for loop is, effectively, the same abstraction that recursion provides in functional programming languages.
@@ -897,6 +905,7 @@ except Exception:
897
905
```
898
906
899
907
### EAFP
908
+
900
909
**EAFP** = Easier to ask forgiveness than permission.
901
910
902
911
As seen in conditions section, exception catching is preferred over checking an object before using.
@@ -906,15 +915,16 @@ Python's performance does not suffer when exception handling.
906
915
Be careful not to abuse this rule in your flow control.
907
916
908
917
## Regular Expressions
909
-
Use raw strings (''r'.+' '') for regex values.
918
+
919
+
Use raw strings (`r'.+'`) for regex values.
910
920
911
921
Raw strings preserve escape characters.
912
922
913
923
This will save you from unexpected behavior.
914
924
915
-
Avoid star wild cards ''\*'', use plus ''+'' instead.
925
+
Avoid star wild cards `*`, use plus `+` instead.
916
926
917
-
Bugs are caused because ''\*'' also allows for zero occurrences.
927
+
Bugs are caused because `*` also allows for zero occurrences.
918
928
919
929
Complex regex should be compiled before using.
920
930
the resulting variable's name should clearly define it's purpose.
0 commit comments