Merge pull request 'naming-convention' (#1) from naming-convention into main

Reviewed-on: https://gitea.com/Lerking/python-crash-course/pulls/1
This commit is contained in:
Lerking
2024-09-22 16:51:53 +00:00
2 changed files with 146 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
"""
Mutable and immutable objects
"""
# A mutable object is an object which can be modified in place.
# In python, everything is an object(class). So creating any variable
# will create a class instance of that object type, with a specific set of methods,
# which can operate on the object. It will also contain the content.
#
# A mutable object is an object which stays the same object, no matter what legal operation you do on it.
# There are basically 3 types of mutable objects in python.
# Lists, dictionaries and sets.
list()
dict()
set()
# What makes these objects mutable, is the fact that you can alter the content without
# altering the object itself. i.e. you can append items to a list or remove items from a list.
# The same mechanics are true for dictionaries and sets.
# Immutable objects are objects that can't be changed.
# These are some of the types which are immutable.
str()
int()
float()
# What characterisses an immutable object, is that the object itself holds the content.
# In other words, 1 value = 1 object. Take the following example.
A: int = 5
B: int = 5
# Here, we have created 2 objects 'A' and 'B', both with the integer value '5'.
# What happens in python is that when creating such objects, it will start by searching
# already created objects to see if an object with this specific value has already been
# created. If python finds that an object of this particular type and value exists, it
# will return a pointer to this object.
# That means that in our example, both 'A' and 'B' are the same object. We can prove that
# with the following. Using id() on the variable(class object) will show the internal
# python id of that object.
print(id(A))
print(id(B))
# So we can see that these objects both points to the same internal object, meaning that
# only 1 int(5) object exists, but we can have multiple varables pointing to it.
# This is exactly what immutable objects are. There can be only 1 of each.
# Now explore the following.
A: int = 5
B: int = 5
print(id(A))
print(id(B))
B += 1 # add 1 to variable 'B'
print(id(A))
print(id(B))
# What happens here?
# Now variable 'A' still points to the int(5) class object. But a new int(6) clas object
# have been created and variable 'B' now points to this object.
# The same is the case with string objects. Each string object can only contain the string
# value, it was created with.

View File

@@ -0,0 +1,87 @@
"""
Basic naming conventions in python.
We will be covering the following.
Variables, functions, classes, methods
"""
"""
Variables - Predefined
"""
# Do's
# 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 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()
# Dont's
# Don't user lowercase, uppercase or snakecase in class names.
# Be careful to give your classes meaningful names. Preferably describing what the class is used for.
# 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.