In Python you create a class with: class

Like: class MyCalculator(object):

Member variables are variables defined within a class.  For example:

class MyCalculator(object):
statement = "Welcome to the calculator"

You can access member variables with dot notation like so:

Frist lets create an instance of the class:

my_calc = MyCalculator()
print(my_calc.statement)

Method

In Python the term method is used to describe a function within a class.  So a method would look like:

class MyCalculator(object):
def __init__(self, number):
self.number = number

In the above example the init method takes the value number.  __init__() methods exist all the time, but by defining it, we can overwrite the default behavior.  Each __init__ function needs to have a self argument.  This gets the object to keep track of itself.  We can add our own defined variables after that though… in this case I used “number.”

The self.number is creating a new member variable of the class.

When adding our own methods to the same class, we get the variables taken in at init automatically.  For example, if I added a method called square, I don’t need to define method in the input – I just leave it as self:

class MyCalculator(object):
def __init__(self, number):
self.number = number

def square(self):
return self.number * 2

Changing Member Variable Values

class MyCalculator(object):
statement = "Welcome to the calculator"

def update_statement(self):
self.statement = "Thank you for using the calculator"

Now if I call it like so:

start_calc = MyCalculator()
print(start_calc.statement)
print(start_calc.update_statement)

The output will be:

Welcome to the calculator
Thank you for using the calculator

Inheritance

Let’s say that our class is the following:

class MyCalculator(object):
def __init__(self, number):
self.number = number

def square(self):
return self.number * 2

Now we add another class called ScientificCalculator and it uses MyCalculator as it’s parent (or base) class.  That means it has access to all the functions and variables within it.  Assume that we want to add a few more values being passed in to our scientific calculator.  We will want to also pass in: coefficient, binomial and partition.

Here’s how it will look:

class MyCalculator(object):
def __init__(self, number):
self.number = number

def square(self):
return self.number * 2

class ScientificCalculator(MyCalculator):
def __init__(self, number, coefficient, binomial, partition):
super(ScientificCalculator, self).__init__(number)
self.coefficient = coefficient
self.binomial = binomial
self.partition = partition

To extend for the base class, the child class calls the base class as the object.  Then the init function references the base class’ argument “number” and adds in the new extra arguments it will take.   We make use of the call “super” to reference the base class with the notation:

super([my current class], self).[method in base class]([argument from base class])

After that we define our variables like normal, using the dot notation (self.coefficient, etc.)

That will allow something like this:

sci_calc = ScientfiicCalculator([value for number],[some value for coefficient],[some value for binomial], some value for partition)

Overriding Methods

If this is my code:

class MyCalculator(object):
def __init__(self, number):
self.number = number

def square(self):
return self.number * 2

class ScientificCalculator(MyCalculator):
def __init__(self, number, coefficient, binomial, partition):
super(ScientificCalculator, self).__init__(number)
self.coefficient = coefficient
self.binomial = binomial
self.partition = partition

I could override the square method of base class, with the ScientificCalculator class by defining it again with a  different value… for example:

class MyCalculator(object):
def __init__(self, number):
self.number = number

def square(self):
return self.number * 2

class ScientificCalculator(MyCalculator):
def __init__(self, number, coefficient, binomial, partition):
super(ScientificCalculator, self).__init__(number)
self.coefficient = coefficient
self.binomial = binomial
self.partition = partition

def square(self):
return "Your Scientific Square is: "+ self.number*2

 

#

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Archives
Categories