0x06 Python - Classes and Objects
0x06 Python - Classes and Objects
PythonOOP
By: Guillaume
Weight: 1
Project over - took place from Nov 21, 2023 5:00 AM to Nov 22, 2023 5:00 AM
In a nutshell…
Background Context
1/7
OOP is a totally new concept for all of you (especially those who think they know about it :)).
It’s VERY important that you read at least all the material that is listed bellow (and skip what
we recommend you to skip, you will see them later in the curriculum).
As usual, make sure you type (never copy and paste), test, understand all examples shown
in the following links (including those in the video), test again etc. The biggest and most
important takeaway of this project is: experiment by yourself OOP, play with it!
Resources
Read or watch:
Learning Objectives
At the end of this project, you are expected to be able to explain to anyone, without the
help of Google:
General
2/7
What is an attribute
What are and how to use public, protected and private attributes
What is self
What is a method
What is the special __init__ method and how to use it
What is Data Abstraction, Data Encapsulation, and Information Hiding
What is a property
What is the difference between an attribute and a property in Python
What is the Pythonic way to write getters and setters in Python
How to dynamically create arbitrary new attributes for existing instances of a class
How to bind attributes to object and classes
What is the __dict__ of a class and/or instance of a class and what does it contain
How does Python find the attributes of an object or class
How to use the getattr function
Copyright - Plagiarism
You are tasked to come up with solutions for the tasks below yourself to meet with the
above learning objectives.
You will not be able to meet the objectives of this or any following project by copying
and pasting someone else’s work.
You are not allowed to publish any content of this project.
Any form of plagiarism is strictly forbidden and will result in removal from the program.
Requirements
General
Allowed editors: vi, vim, emacs
All your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version
3.8.5)
All your files should end with a new line
The first line of all your files should be exactly #!/usr/bin/python3
A README.md file, at the root of the folder of the project, is mandatory
Your code should use the pycodestyle (version 2.8.*)
All your files must be executable
The length of your files will be tested using wc
All your modules should have a documentation (python3 -c
'print(__import__("my_module").__doc__)')
All your classes should have a documentation (python3 -c
'print(__import__("my_module").MyClass.__doc__)')
3/7
All your functions (inside and outside a class) should have a documentation (python3
-c 'print(__import__("my_module").my_function.__doc__)' and python3 -c
'print(__import__("my_module").MyClass.my_function.__doc__)')
A documentation is not a simple word, it’s a real sentence explaining what’s the
purpose of the module, class or method (the length of it will be verified)
More Info
Documentation is now mandatory! Each module, class, and method must contain
docstring as comments. Example Google Style Python Docstrings
Quiz questions
Question #0
class User:
id = 89
name = "no name"
__password = None
Question #1
Question #2
4/7
class User:
id = 89
name = "no name"
__password = None
Question #3
class User:
id = 89
name = "no name"
__password = None
Question #4
Question #5
5/7
>>> class User:
>>> id = 89
>>> name = "no name"
>>> __password = None
>>>
>>> def __init__(self, new_name=None):
>>> self.is_new = True
>>> if new_name is not None:
>>> self.name = new_name
>>>
>>> u = User("John")
>>> u.name
Question #6
class User:
id = 89
name = "no name"
__password = None
Question #7
6/7
7/7