Class

A class is a collection of attributes and methods that can be instantiated.

class Monster: 
  hit_points = 1
  color = 'yellow'
  weapon = 'sword'

Create an instance of the class

>>> from monster import Monster
>>> jubjub = Monster() 
>>> type(jubjub)
<class 'monster.Monster'>
>>> type('a')
<class 'str'>

Attributes

>>> jubjub.hit_points
1
>>> jubjub.weapon
'sword'
>>> jubjub.hit_points = 5
>>> jubjub.hit_points
5
>>> jabberwock = Monster()
>>> jabberwock.hit_points
1

Methods

They are functions that belong to a class

class Monster:
  hit_poits = 1 
  color = 'yellow'
  weapon = 'sword'
  sound = 'roar'

  def battlecry(self):		
    return self.sound.upper()

Methods MUST take the self variable as an argument. self represents the the instance you’re calling the method on.
Inside of our Method, we use the self variable to get information from the current instance.

>>> jubjub = Monster()
>>> jubjub.battlecry()
'ROAR'
>>> jubjub.sound = 'tweet'
>>> jubjub.battlecry()
'TWEET'

Instantiating Objects

__init( )__ A method that helps us control things at the beginning of the object instantiation.

  

Class Creation – Type 1

Setting attributes inside __init( )__.

Using this type of initiation:
1. You have to remember the order of the arguments.
2. You have to give all the arguments values at object initiation.

class Monster:
  def __init__(self, hit_points, weapon, color, sound):
    self.hit_points = hit_points
    self.weapon = weapon
    self.color = color
    self.sound = sound
    # __init__ doesn't need to return anything, it just needs to set the attributes.

  def battlecry(self):
    return self.sound.upper()
>>> from monster import Monster 
>>> slimey = Monster(5, 'sword', 'blue', 'glug')
>>> slimey.color
'blue'
>>> slimey.hit_points
5
>>> slimey.battlecry()
'GLUG'
>>> slimey.hit_points

 

Class Creation – Type 2

Using default argument values.

Here, you don’t have to give all the arguments values at object initiation, because there are already default values.
However, ..
1. You have to remember the order of the arguments at object initiation.
2. You’re assigning values to the attributes in the method twice; once in the argument the second inside the __init__ function.

class Monster:
  def __init__(self, hit_points=5, weapon='sword', color='yellow', sound='roar'):
    self.hit_points = hit_points
    self.weapon = weapon
    self.color = color
    self.sound = sound

  def battlecry(self):
    return self.sound.upper()

 

Class Creation – Type 3

Using Dictionary packing and unpacking. This is the smartest way to instantiate objects.

1. You assign attributes only once.
2. You have default values set for the unset attributes.

class Monster:
  def __init__(self, **kwargs):
    self.hit_points = kwargs.get('hit_points', 1)
    self.weapon = kwargs.get('weapon', 'sword')
    self.color = kwargs.get('color', 'yellow')
    self.sound = kwargs.get('sound', 'roar')

  def battlecry(self):
    return self.sound.upper()
>>> from monster import Monster
>>> jubjub = Monster(color='red', sound='tweet') # Done, in one single line.
>>> jubjub.battlecry()
'TWEET'