From f4a00cb09215e2ca64284c67d7dc05a1f66c4c1a Mon Sep 17 00:00:00 2001 From: Lerking Date: Sun, 22 Sep 2024 09:33:04 +0000 Subject: [PATCH 1/3] Add basics/naming_convention.py --- basics/naming_convention.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 basics/naming_convention.py diff --git a/basics/naming_convention.py b/basics/naming_convention.py new file mode 100644 index 0000000..ece0df7 --- /dev/null +++ b/basics/naming_convention.py @@ -0,0 +1,11 @@ +""" +Basic naming conventions in python +""" + +# 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: + F.write() + +#Dont's From a419f343bcbbde099360cca596d824b00c38ca29 Mon Sep 17 00:00:00 2001 From: Jan Lerking Date: Sun, 22 Sep 2024 15:18:02 +0200 Subject: [PATCH 2/3] Update. JL --- basics/naming_convention.py | 82 ++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/basics/naming_convention.py b/basics/naming_convention.py index ece0df7..e5ef0a3 100644 --- a/basics/naming_convention.py +++ b/basics/naming_convention.py @@ -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. \ No newline at end of file From d5060c275276243960d941c899c832a8528fe694 Mon Sep 17 00:00:00 2001 From: Jan Lerking Date: Sun, 22 Sep 2024 18:48:37 +0200 Subject: [PATCH 3/3] im-/mutable objects added. /JL --- basics/mutable_and_immutable_objects.py | 59 +++++++++++++++++++++++++ basics/naming_convention.py | 4 ++ 2 files changed, 63 insertions(+) create mode 100644 basics/mutable_and_immutable_objects.py diff --git a/basics/mutable_and_immutable_objects.py b/basics/mutable_and_immutable_objects.py new file mode 100644 index 0000000..d724728 --- /dev/null +++ b/basics/mutable_and_immutable_objects.py @@ -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. diff --git a/basics/naming_convention.py b/basics/naming_convention.py index e5ef0a3..3bf21e8 100644 --- a/basics/naming_convention.py +++ b/basics/naming_convention.py @@ -67,6 +67,10 @@ class MyClass: 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/")