mirror of
https://gitea.com/Lerking/python-crash-course.git
synced 2025-12-16 11:30:07 +01:00
Update. JL
This commit is contained in:
@@ -1,11 +1,83 @@
|
||||
"""
|
||||
Basic naming conventions in python
|
||||
Basic naming conventions in python.
|
||||
We will be covering the following.
|
||||
Variables, functions, classes, methods
|
||||
"""
|
||||
|
||||
"""
|
||||
Variables - Predefined
|
||||
"""
|
||||
# Do's
|
||||
# Use lowercase and snakecase for functions and methods i.e. def save_file()
|
||||
def save_file(file: str) -> None:
|
||||
with open(file, "w") as F:
|
||||
# Use UPPERCASE in combination with snakecase
|
||||
HOME_FOLDER = "/home/me"
|
||||
|
||||
# Dont's
|
||||
# Do not use lowercase for variable names.
|
||||
home_folder = "/home/me"
|
||||
|
||||
# When using UPPERCASE letter for variables, it becomes extremely easy to
|
||||
# identify variable names in your code, at a single glance.
|
||||
# If you use lowercase letter for variable names, they will be harder to see
|
||||
# in your code, as they would be easily misidentified as function names.
|
||||
|
||||
"""
|
||||
Functions
|
||||
"""
|
||||
# Do's
|
||||
# Use lowercase in combination with snakecase for functions.
|
||||
# Use describing names for functions, which makes it easier to identify what
|
||||
# the given function is used for.
|
||||
# Always use types in function headers.
|
||||
# Always show th return type in function headers.
|
||||
# Using types in the function header makes it a lot easier for others to
|
||||
# see how your function is supposed to work. A further effect, is that linters
|
||||
# will show warnings correctly.
|
||||
def save_file(filename: str) -> None:
|
||||
with open(filename, "w") as F:
|
||||
F.write()
|
||||
|
||||
#Dont's
|
||||
# Dont's
|
||||
# Dont use CamelCase or UPPERCASE in function names.
|
||||
# Dont leave out types in your function headers.
|
||||
def SaveFile(filename):
|
||||
with open(filename, "w") as F:
|
||||
F.write()
|
||||
|
||||
def SAVEFILE(filename):
|
||||
with open(filename, "w") as F:
|
||||
F.write()
|
||||
|
||||
# Using CamelCase for function names, could confuse them with class names.
|
||||
# Using UPPERCASE letters for function names, could confuse them with variable names.
|
||||
|
||||
"""
|
||||
Classes
|
||||
"""
|
||||
# Do's
|
||||
# Use CamelCase for class names
|
||||
# Functions defined inside a class are called methods. Differentiating between function
|
||||
# and method makes it easier for people to know what you're refering to.
|
||||
# A method wil ALWAYS take 'self' as it's first parameter. 'self' is the actual class reference
|
||||
# this method is operating on.
|
||||
class MyClass:
|
||||
def __init__(self, folder: str = ""):
|
||||
self.folder = folder
|
||||
|
||||
def save_file(self, filename: str) -> None:
|
||||
with open(self.folder + filename, "w") as F:
|
||||
F.write()
|
||||
|
||||
# You can create several instances of the same class. Which is where 'self' comes into play.
|
||||
myclass = MyClass()
|
||||
myclass_1 = MyClass(folder = "/home/me/")
|
||||
myclass.save_file("test.txt")
|
||||
myclass_1.save_file("my_file.txt")
|
||||
|
||||
"""
|
||||
Methods
|
||||
"""
|
||||
# The same rules applies to methods as functions as they are in essense the same.
|
||||
# The difference between functions and methods, lies in where the were defined.
|
||||
# If a function is defined inside a class, it becomes a method. That means a method
|
||||
# of functionality for it's defining class.
|
||||
# Any other function definitions are just functions.
|
||||
Reference in New Issue
Block a user