The goal is to be proficient in the object-oriented aspects of Python. In particular, I aimed to understand the Eliza.py code and its syntax. This means I thoroughly understand how the code produces the outputs given the user input. Further, I will demonstrate in this page some of the python practices that I did, using automatetheboringstuff.com and youtube resources. Being proficient and fluent in python is a critical and imperative skill in any potential direction I am going careerwise. It was a very enjoyable task to get a clear grasp of how this piece of code works.
In fortnight 1, we explored the axi-plotter and the chatbot. https://github.com/jezhiggins/eliza.py/blob/main/eliza.py
They were both done using Python and shell scripts. The goal stemming from the fortnight 1 build course was to consistently work on python and the shell script skills.
- The use of keyword self
- The use of keys and values, meaning, the dictionary
- The use of regex function re.compile
The keyword, ‘self’ is frequently used in Python. It represents the instance of the class. It is similar to ‘this’ in Java or C++. In Java and C++, ‘this’ is not mandated, but ‘self’ is mandated in python. What I mean by that is as follows:
Notice in func1, self was not written in as an argument, when in func2, self was written in as a default argument.
Notice when calling func2, self was not written as an argument, and yet it did not error.
func1 errors because when it was defined, ‘self’ was not written as an argument.
The reason func2 did not error even though when it was called self was not written, is because python passes ‘self’ implicitly automatically. In summary, the conclusion is, that when defining a function, it is important to write ‘self’, whereas when calling the function it does not make a difference if you write ‘self’ or not.
By checking the id of the instance, the memory address of the instance f is known to be 43219856.
This is what happens when we create an instance using Foo() in the memory.
Notice we did not call ‘self’ as an argument to call func2() and yet it did not error.
The newly created Foo instance now has a different memory address id.
Notice, in the above snippet, the fun1 is not erroring. This is because ‘Foo’ is the class. In summary, instance.function(self) requires ‘self’, whereas class.function() does not.
Now let’s use the class, not an instance to call func2.
It errors. Then how do can we use the class and still use func2?
Create another instance, f3 of Foo. Notice the new memory address.
Class.method(instance) is the correct syntax. This makes sense. as ‘self’ refers to the instance itself. f3 in this case is the instance that needs to be passed. In conclusion, you can use f3, which is the newly created instance. Remember the following grammar(syntax).
As you can probably tell intuitively, ‘self’ refers to the instance of the class itself. ‘Self’ allows the coder to access the attributes and methods in the class. It binds the attributes with the given arguments. This is because python does not use @ to refer to the instance syntax. In python, we have methods that make the instance be passed automatically but not received automatically. Whenever you see the keyword ‘self’, you will also see def __init__(self, something)
Python Object Oriented Project: Banking System
In Shell scripting, echo is equivalent to print in python.