Skip to content

Latest commit

 

History

History

0x08-python-more_classes

img

More classes

Intro

Lets dig a little deeper into classes in python.

Resources

Read or watch the following

  1. Object Oriented Programming
  2. OOP in Python(Please be careful: in most of the following paragraphs, the author shows the way you should not use or write a class, in order to help you better understand some concepts and how everything works in Python 3. Make sure you read only the following paragraphs: “General Introduction,” “First-class Everything,” “A Minimal Class in Python,” “Attributes,” “Methods,” “The __init__ Method,” “Data Abstraction, Data Encapsulation, and Information Hiding,” __str__ - and __repr__ -Methods,” “Public- Protected- and Private Attributes,” & “Destructor”)
  3. Class vs Instance attributes
  4. Class method vs static methods
  5. Properties vs setters vs getters
  6. __str__ vs __repr__

Learning objectives

By the end of this project, you should be able to explain to anyone the following concepts without the help of Google

  • What is OOP
  • “first-class everything”
  • What is a class
  • What is an object and an instance
  • What is the difference between a class and an object or instance
  • 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
  • What are the special __str__ and __repr__ methods and how to use them
  • What is the difference between __str__ and __repr__
  • What is a class attribute
  • What is the difference between a object attribute and a class attribute
  • What is a class method
  • What is a static method
  • How to dynamically create arbitrary new attributes for existing instances of a class
  • How to bind attributes to object and classes
  • What is and what does contain __dict__ of a class and of an instance of a class
  • How does Python find the attributes of an object or class
  • How to use the getattr function

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
Show quiz

Question #0

What do these lines print?

class User:
    id = 1

User.id = 98
u = User()
u.id = 89
print(User.id)
  • 89
  • None
  • 98
  • 1

Question #1

What do these lines print?

class User:
    id = 1

u = User()
u.id = 89
User.id = 98
print(u.id)
  • 89
  • None
  • 1
  • 98

Question #2

What is __doc__

  • Creates man file
  • The string documentation of an object (based on docstring)
  • Prints the documentation of an object

Question #3

What do these lines print?

class User:
    id = 1

u = User()
u.id = 89
print(u.id)
  • 89
  • 98
  • None
  • 1

Question #4

What do these lines print?

class User:
    id = 1

u = User()
print(u.id)
  • 89
  • 98
  • None
  • 1

Question #5

What is __str__

  • Instance method that prints an “informal” and nicely printable string representation of an instance
  • Instance method that returns an “informal” and nicely printable string representation of an instance
  • Instance method that returns the dictionary representation of an instance

Question #6

What is __repr__

  • Instance method that returns the dictionary representation of an instance
  • Instance method that prints an “official” string representation of an instance
  • Instance method that returns an “official” string representation of an instance

Question #7

What do these lines print?

class User:
    id = 1

u = User()
u.id = 89
User.id = 98
print(User.id)
  • 89
  • None
  • 1
  • 98

Question #8

What is __init__?

  • The instance method called when a new object is created
  • A class attribute
  • A class method
  • The instance method called when a class is called for the first time

Question #9

What do these lines print?

class User:
    id = 1

User.id = 98
u = User()
u.id = 89
print(u.id)
  • 89
  • 98
  • None
  • 1

Question #10

What do these lines print?

class User:
    id = 1

u = User()
User.id = 98
print(u.id)
  • 89
  • None
  • 1
  • 98

Question #11

What do these lines print?

class User:
    id = 1

print(User.id)
  • 89
  • 1
  • 98
  • None

Question #12

What do these lines print

class User:
    id = 1

User.id = 98
u = User()
print(u.id)
  • 89
  • None
  • 1
  • 98

Question #13

What is __del__?

  • Instance method called when an instance is deleted
  • Instance method that removes the last character of an instance
  • Instance method that prints the memory address of an instance