Classes - Python: Programming Language for Beginners - Learn In A Day! (2015)

Python: Programming Language for Beginners - Learn In A Day! (2015)

Chapter 10. Classes

Limitations of functions:

· Don't store any info as variables.

· Every time it runs, it has to start freshly.

· Gives single outputs. Cannot generate multiple outputs.

· We need to search an alternative so that we can have all the variables and functions with close similarities at one place grouped so that interaction among them is possible.

· Object oriented programming is used to manage the problem. It keeps both together in such a manner that they work together and can be manipulated when needed. Here classes can be used.

Creating a Class

Class is defined as a blueprint. You can also create objects for that class. Technically called as instance.

Example -defining class

# Defining a class

class class_name:

[statement 1]

[statement 2]

[statement 3]

[etc]

Example -Example of a Class

#An example of a class

class Shape:

def __init__([i]self,x,y):

self.x = x

self.y = y

description = "This shape has not been described”

author = "Nobody has”

def area(self):

return self.x * self.y

def perimeter(self):

return 2 * self.x + 2 * self.y

def describe(self,text):

self.description = text

def authorName(self,text):

self.author = text

def scaleSize(self,scale):

self.x = self.x * scale

self.y = self.y * scale

Here we have created the description and the operations can be performed on the shape. The shape has a width (x), a height (y), area and the perimeter (area(self) and perimeter(self) respectively). Defining a class doesn’t mean running a code.

The function called __init__is executed when we create an instance of Shape.

‘self’ is used to refer to things in the class which belong to itself. self is the 1st factor in a function is defined in a class. (To access the variables and functions elsewhere or in the class, its name should be preceded with a self and a ‘.’ (e.g. self.variable_name).

Using a class:

Example -Creating class

rectangle = Shape(100,45)

The __init__ function comes to play when we make an instance in a class by giving its name (in this case, Shape) and then, in parenthesis, the values to pass the__init__ function. It runs and then gives out an instance of the class, which is assigned for the name ‘rectangle’ i.e. the class name.

Example -accessing attributes from outside an instance

#finding the area of your rectangle:

print rectangle.area()

#finding the perimeter of your rectangle:

print rectangle.perimeter()

#describing the rectangle

rectangle.describe("A wide rectangle”)

#making the rectangle 50% smaller

rectangle.scaleSize(0.5)

As seen, self is being used within the class. Its given name is applied when it’s outside of the class. We can do that to access functions that are outside. We are not restricted to use a single instance. We can create multiple instances for the same class as well.

Lingo

Object-oriented-programming has a lingo which it’s related with.

When we describe class, we define it as

Encapsulation: Ability to group akin functions and variables

A variable in class is called an Attribute

A function in class is called a method

A class is called as 'data structure' if it stores data and also the techniques to process the data.

Inheritance

Inheritance is a phenomenon of acquiring the properties of the parent class. We can also add additional features if required.

Example -the Shape class

class Shape:

def __init__(self,x,y):

self.x = x

self.y = y

description = "This shape has not been described yet"

author = "Nobody has claimed to make this shape yet"

def area(self):

return self.x * self.y

def perimeter(self):

return 2 * self.x + 2 * self.y

def describe(self,text):

self.description = text

def authorName(self,text):

self.author = text

def scaleSize(self,scale):

self.x = self.x * scale

self.y = self.y * scale

Now, if we want to define another class, say square, based on the previous class called Shape, we can do this:

Example -Using inheritance

class Square(Shape):

def __init__(self,x):

self.x = x

self.y = x

The name of the parent class from which we have inherited the properties is written in the parenthesis.

Pointers & Dictionaries Used in Classes

In case of class instances the name that is at the left becomes the class instance for the right. So in instance2 = instance1, instance2 is said 'pointing' to instance1.

In python doing this doesn’t require pointers.

Example -Dictionaries of Classes

dictionary = {}

dictionary["DoubleSquare 1"] = DoubleSquare(5)

dictionary["long rectangle"] =Shape(600,40)

print dictionary["long rectangle"].area().