India

ZestyBeanz Technologies Pvt Ltd 4th Floor, Nila, Technopark Thiruvananthapuram,
India – 695581
Phone: +91 471 4063254
Fax : +91 471 2700171

   .

ZestyBeanz Technologies Pvt Ltd
61/3236, Manikkath Cross Road
Ravipuram, Kochi, India - 682016
Phone: +91 484 4063254

  UAE

Zesty Labs
Office # 2003, Millennium Plaza Building
Sheikh Zayed Rd, Dubai, UAE
Phone: +971 4333 2222​
Mobile: +971-52-7553466

  Germany​

ZestyBeanz Technologies GmbH
Reuterstraße 1
90408 Nürnberg
Fon: +49 911 4801 444
Fax: +49 911 4801 445

Object Oriented Python

Contact Form


vijayan's picture

Object Oriented Python

Introduction

 

As of any Programming language python has got the feature of Object Oriented Programming. And as of any other features of python its the simplest of all other programming languages' syntax. It provides object oriented features like inheritance, abstraction, encapsulation etc.

 

The Power of Objects

As you can see objects combine data and the methods used to work with the data. This means it's possible to wrap complex processes - but present a simple interface to them. How these processes are done inside the object becomes a mere implementation detail. Anyone using the object only needs to know about the public methods and attributes. This is the real principle of encapsulation. Other parts of your application (or even other programmers) can use your classes and their public methods - but you can update the object without breaking the interface they use.

You can also pass around objects instead of just data. This is one of the most useful aspects of object oriented programming. Once you have a reference to the object you can access any of the attributes of the object. If you need to perform a complex group of operations as part of a program you could probably implement it with procedures and variables. You might either need to use several global variables for storing state (which are slower to access than local variables and not good if a module needs to be reusable within your application) - or your procedures might need to pass around a lot of variables.

If you implement a single class that has lots of attributes representing the state of your application, you only need to pass around a reference to that object. Any part of your code that has access to the object, can also access its attributes.

The main advantage of objects though is that it is a useful metaphor. It fits in with the way we think. In real life objects have certain properties and interact with each other. The more our programming language fits in with our way of thinking, the easier it is to use it to think creatively.

 

 

Defining a new class

 

class MyClass:

def __init__(self,arg1,arg2):

#object initializer called may be treated as contructors in c++ terminology

#this can be used for initializing the member variables of the object

self.a=arg1

self.b=arg2

 

Now we have created a simple object in python which can be used as follows

 

Creating Object Instances

 

>> MyClassInstance=MyClass(10,12)

 

>> print MyClassInstance.a

 

Here the variables a,b can be called as attributes and __init__ is the initializer method of the object MyClass. '.' operator can be used to get the value of the attribute of an object from its instance. You can examine all the methods and attributes that are associated with an object using the dir command as

 

>>dir(MyClass)

 

 

boss's picture

nice post

nice post