PYTHON - OBJECT ORIENTED PROGRAMMING - Complete Guide For Python Programming (2015)

Complete Guide For Python Programming (2015)

PYTHON - OBJECT ORIENTED PROGRAMMING

Learning Python Classes

In OOP (object-oriented programming), the class is the most basic component. OOP is very powerful tool and many Python libraries and APIs uses classes, and so you should know what classes are? How they work? And how to create them? One thing to remember about Python and OOP is that it’s not mandatory to use objects in your code.

Now the question is how are Classes Better?

Let’s take an example, suppose you want to calculate the velocity of a car in a two-dimensional plane using functions. And you now want to make a new program to calculate the velocity of an airplane in three dimensions, and then you’ll have to rewrite the many of the functions to make them work for the vertical dimension, especially to map the object in a 3-D space. In that case classes are used. Classes let you define an object once, and then you can reuse it multiple times. You need to give it a base function, and then build upon that method to redefine it as necessary.

Improving Your Class Standing

Some concepts of classes are given below. Have a look at them:

- Classes have a definite namespace, just like modules. Trying to call a class method from a different class will give you an error unless you qualify it, e.g. spamClass.eggMethod().

- Classes support multiple copies. Classes have two different objects: class objects and instance objects. Class objects are used to give the default behavior and are used to create instance objects. Instance objects are the objects that actually do the work in your program. You can have as many instance objects of the same class object as you need.

- Each instance object has its own namespace but also inherits from the base class object. This means each instance has the same default namespace components as the class object, but additionally each instance can make new namespace objects just for itself.

- Classes can define objects that respond to the same operations as built-in types. So, class objects can be sliced, indexed, con- catenated, etc. just like strings, lists, and other standard Python types. This is because everything in Python is actually a class object; we aren’t actually doing anything new with classes, we’re just learning how to better use the inherent nature of the Python language.

So What Does a Class Look Like?

See here you class look like in python:

Defining a class

>>> class Item : #define a classobject

... def setName (self,value) : #define class methods

... self.name = value #selfidentifiesaparticular instance

... def display (self) :

... print self.name #print the data for a particular instance

Creating class instances

>>> x = Item ( )

>>> y = Item ( )

>>> z = Item ( )

Adding data to instances

>>> x.setName ("Hello, This is Python book.")

>>> y.setName ("I am a quick learner.")

>>> z.setName ("It is worth buying this book.")

Displaying instance data

>>> x.display ( )

Hello, This is Python book.

>>> y.display ( )

I am a quick learner.

>>> z.display ( )

It is worth buying this book.