1. Objects and Classes

1.1. Object Paradigm

  • Model world as objects that interacts with each other

class

Templates for objects.

instance
object

Object created from class.

method

Function inside the class.

property
attribute
field

Variable inside the class.

1.2. Classes

  • Capitalized CamelCase name convention

  • Classes are templates for objects

Listing 268. Defining class. Classes should have capitalized name
class Iris:
    pass
Listing 269. Multi-word class names should use CamelCase
class IrisSetosa:
    pass

1.2.1. Classes vs Instances

  • Instances are also known as Objects

  • Two newlines between class and code

  • snake_case names

../_images/blueprint.png

Figure 33. Intuition definition: Class is a blueprint, instances are homes made from this plan. Image source: [ImgFre19]

Listing 270. One class and one instance
class Iris:
    pass


flower = Iris()
Listing 271. One class and three instances
class Iris:
    pass


setosa = Iris()
versicolor = Iris()
virginica = Iris()
Listing 272. Three classes and four instances
class IrisSetosa:
    pass

class IrisVersicolor:
    pass

class IrisVirginica:
    pass


iris_setosa1 = IrisSetosa()
iris_setosa2 = IrisSetosa()
iris_versicolor = IrisVersicolor()
iris_virginica = IrisVirginica()

1.3. Methods

  • Methods are functions in the class

  • First argument is always instance (self)

  • While calling function you never pass self

1.3.1. Methods without arguments

Listing 273. Methods without arguments
class Iris:
    def latin_name(self):
        print(f'Latin name: Iris Setosa')


flower = Iris()

flower.latin_name()
# Latin name: Iris Setosa

1.3.2. Methods with argument

Listing 274. Methods with arguments
class Iris:
    def latin_name(self, species):
        print(f'Latin name: {species}')


flower = Iris()

flower.latin_name(species='Iris Setosa')
# Latin name: Iris Setosa

flower.latin_name('Iris Setosa')
# Latin name: Iris Setosa

flower.latin_name()
# TypeError: latin_name() missing 1 required positional argument: 'species'

1.3.3. Methods with arguments with default value

Listing 275. Methods with arguments with default value
class Iris:
    def latin_name(self, species='unknown'):
        print(f'Latin name: Iris {species}')


flower = Iris()

flower.latin_name(species='Iris Setosa')
# Latin name: Iris Setosa

flower.latin_name('setosa')
# Latin name: Iris Setosa

flower.latin_name()
# Latin name: unknown

1.3.4. Methods calling other methods

Listing 276. Methods calling other methods
class Iris:
    def get_name(self):
        return 'Iris Setosa'

    def latin_name(self):
        name = self.get_name()
        return f'Latin name: {name}'


flower = Iris()

flower.latin_name()
# Latin name: Iris Setosa

1.4. Initializer Method

  • It's a first method run after object is initiated

  • All classes has default __init__()

  • __init__() is not a constructor!

1.4.1. Initializer method without arguments

Listing 277. Initializer method without arguments
class Iris:
    def __init__(self):
        print('Latin name: Iris Setosa')


flower = Iris()
# Latin name: Iris Setosa

1.4.2. Initializer method with arguments

Listing 278. Initializer method with arguments
class Iris:
    def __init__(self, species):
        print(f'Latin name: {species}')


setosa = Iris('Iris Setosa')
# Latin name: Iris Setosa

virginica = Iris(species='Iris Virginica')
# Latin name: Iris Virginica

iris = Iris()
# TypeError: __init__() missing 1 required positional argument: 'species'

1.5. Fields

  • Fields are also known as "Properties" or "Attributes"

  • snake_case name convention

  • Fields should be defined only in __init__() method

  • Fields store information for instances

  • Access field values using . (dot) notation

1.5.1. Fields with constant values

Listing 279. Fields with constant values
class Iris:
    def __init__(self):
        self.sepal_length = 5.1
        self.sepal_width = 3.5
        self.petal_length = 1.4
        self.petal_width = 0.2
        self.species = 'setosa'


flower = Iris()

print(flower.sepal_length)  # 5.1
print(flower.sepal_width)   # 3.5
print(flower.species)       # 'setosa'

1.5.2. Initializing fields on instance creation

Listing 280. Initializing fields on instance creation
class Iris:
    def __init__(self, species):
        self.species = species


setosa = Iris(species='setosa')
print(setosa.species)
# setosa

virginica = Iris('virginica')
print(virginica.species)
# virginica

versicolor = Iris()
# TypeError: __init__() missing 1 required positional argument: 'species'
Listing 281. Method argument with default value
class Iris:
    def __init__(self, species=None):
        self.species = species


versicolor = Iris()
print(versicolor.species)
# None

1.6. Methods accessing fields

Listing 282. Methods accessing fields
class Iris:
    def __init__(self, species='unknown'):
        self.species = species

    def latin_name(self):
        print(f'Latin name is: {self.species}')


setosa = Iris('Iris Setosa')
setosa.latin_name()
# Latin name is: Iris Setosa

iris = Iris()
iris.latin_name()
# Latin name is: unknown

1.7. Assignments

1.7.1. Classes and instances

  1. Stwórz klasę Temperature

  2. Klasa ma pamiętać wprowadzoną wartość

  3. Stwórz trzy instancje z wartościami podawanymi przy inicjalizacji:

    • instancja celsius z temperaturą 36.6

    • instancja fahrenheit z temperaturą 97.88

    • instancja kelvin z temperaturą 309.75

  4. Wypisz temperatury na ekranie

1.7.2. Data Modeling

  1. Zamodeluj dane w programie za pomocą klas:

    • Jan, Twardowski, 1961-04-12

    • Mark, Watney, 1969-07-21

    • Kennedy Space Center, Merritt Island, FL

    • Johnson Space Center, Houston, TX

    • Jet Propulsion Laboratory, Pasadena, TX

  2. Stwórz instancje dla każdego wpisu

1.7.3. Methods

  1. Stwórz klasę Iris z polami:

    • sepal_length: float,

    • sepal_width: float,

    • petal_length: float,

    • petal_width: float,

    • species: str.

  2. Napisz metodę total() wyliczającą sumę atrybutów numerycznych obiektu

  3. Napisz metodę average() wyliczającą średnią powyższych właściwości

  4. Stwórz obiekt setosa z pomiarami podawanymi przy inicjalizacji:

    • sepal_length: 5.4

    • sepal_width: 3.9

    • petal_length: 1.3

    • petal_width: 0.4

  5. Stwórz drugi obiekt virginica z pomiarami podawanymi przy inicjalizacji:

    • sepal_length: 5.8

    • sepal_width: 2.7

    • petal_length: 5.1

    • petal_width: 1.9

  6. Wyświetl na ekranie nazwę gatunku oraz sumę i średnią z pomiarów dla obu instancji