Python Zero to Heros

Online Absolute Beginner Python Tutorials 

Every Sunday 2pm (UK time/ BST)

Get this slide deck: https://slides.com/cheukting_ho/python-classes

Recap

Python objects - int, float, str, list, dict, bool

Control flows - if-else, for loop, while loop

Functions and modeuls

 

Any Questions?

Classes

Creating a new class creates a new type of object, allowing new instances of that type to be made. - docs.python.org

 

Classes provide features of Object Oriented Programming

class Basket:
  def __init__(self):
    self.content = []
    
  def add_item(self, item):
    self.content.append(item)
    print(f"{item} is added to basket.")
    

Classes

Let's create an instance and have a look at dir(my_basket)

 

my_basket = Basket()
dir(my_basket)
    

my_basket is an instances that has attribute and methods bound togehter

 

let's go deeper into scpoes

Scopes

def scope_test():
    def do_local():
        spam = "local spam"

    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"

    def do_global():
        global spam
        spam = "global spam"

    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

Classes

Let's try this:

class Basket:
  
  content = []
  
  def __init__(self):
    pass
    
  def add_item(self, item):
    self.content.append(item)
    print(f"{item} is added to basket.")
    
my_basket = Basket()
your_basket = Basket()

my_basket.add_item("toilet papers")
print("My basket: ", my_basket.content)
your_basket.add_item("coffee beans")
print("Your basket: ", your_basket.content)

Classes

use other instance methods in funcitons

Can we have an add_items(self, *args) method?

class Basket:
  
  def __init__(self):
    self.content = []
    
  def add_item(self, item):
    self.content.append(item)
    print(f"{item} is added to basket.")
    
  def add_items(self, *args):
    for item in args:
      self.add_item(item)
    print(f"{len(arg)} items are added.")
    
my_basket = Basket()

my_basket.add_items("toilet papers", "organes", "milk")
print("My basket: ", my_basket.content)

Classes

make aliases e.g. add_multi_items(self, *args)

class Basket:
  
  def __init__(self):
    self.content = []
    self.add_milti_items = self.add_items
    
  def add_item(self, item):
    self.content.append(item)
    print(f"{item} is added to basket.")
    
  def add_items(self, *args):
    for item in args:
      self.add_item(item)
    print(f"{len(arg)} items are added.")
    
my_basket = Basket()

my_basket.add_milti_items("toilet papers", "organes", "milk")
print("My basket: ", my_basket.content)

Private Variables

Single underscore e.g. _update() - not for public use

Double underscore aka Dunder e.g. __init__() - can't be public use

Try this:

class Basket:
  
  def __init__(self):
    self.content = []
    
  def __add_item(self, item):
    self.content.append(item)
    print(f"{item} is added to basket.")
    
my_basket = Basket()

my_basket.__add_item("toilet papers")
print("My basket: ", my_basket.content)

What Else?

 

Details about classes and instances 

(expires 14th May!!) - Naomi Ceder

 

Detail about object model and inheritance 

- Michael Foord

 

Iterators & Generators 

(expires 21st May!!) - Naomi Ceder

Let's write some programs using classes

 

Simple E-comerce similator ( e-shop.py)

 

https://github.com/Cheukting/python02hero/tree/master/2020-05-10-python-classes

Homework 📝

 

Can you improve our example?

 

Can we now have the records the previous orders of the customers?

 

https://github.com/Cheukting/python02hero/tree/master/2020-05-10-python-classes

Next week:
Testing in Python

Sunday 2pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 1pm