Skip to main content

Python Object Oriented

Python Object Oriented
Python is an Object oriented programming Language. This means there exists a concept called ‘class’ that let’s programmer structure the codes of a software in a fashioned way. Because of the use of classes and objects, the programming became easy to understand and code.

Defining Class And Object

A class is a technique to group functions & data members and put them inside a container so that they can be accessed later by using dot (.) operator. Objects are the basic runtime entities of object oriented programming. It defines the instance of a class. Objects get their variables & functions from classes & the class we will be creating are the templates made to create object.

Object Oriented Terminologies

  • class: Classes are user defined data type that is used to encapsulate data and associated functions together. It also helps in binding data together into a single unit.
  • Data Member: A variable or memory location name that holds value to does specific task within a program.
  • Member Function: They are the functions; usually a block of code snippet that is written to re-use it.
  • Instance variable: A variable that is defined inside a method of a class.
  • Function Overloading: This technique is used to assign multiple tasks to a single function & the tasks are performed based on the number of argument or the type of argument the function has.
  • Operator Overloading: It is the technique of assigning multiple function/tasks to a particular operator.
  • Inheritance: It is the process of acquiring the properties of one class to another i.e. one class can acquire the properties of another.
  • Instantiation: It is the technique of creating an instance of a class.

Program For Class In Python

Example:
#!/usr/bin/python

class karl :
varabl
= 'Hello'

def function(self) :
print "This is a message Hello"
Another program to explain functions inside class:
Example:
#!/usr/bin/python

class karl(object) :
def __init__(self, x, y):
self.x = x
self.y = y

def sample(self) :
print " This is just a sample code"
In the above code, we created a class name karl using ‘class’ keyword. And two functions are used namely __init__() (for setting the instance variable) & sample(). Classes are used instead of modules because programmers can take this class ‘karl’ & use it or modify it as many times as we want & each one won’t interfere with each other. Importing a module brings the entire program into use.

Creating Objects (Instance Of A Class)

Let’s see an example to show how to create an object:
Example:
#!/usr/bin/pythonclass student:
class student:
def __init__(self, roll, name):
self.r = roll
self.n = name
print ((self.n))

#...
stud1
= student(1, "Alex")
stud2
= student(2, "Karlos")

print (("Data successfully stored in variables"))
Output:
Alex
Karlos
Data successfully stored in variables

Accessing Object Variables

We can access the object’s variable using dot (.) operator.
The syntax is:
my object_name.variable_name
Example:
print object.varabl

Accessing Attributes

Object attributes can also be accessed by using dot operator.
Example:
stud1.display()
stud2
.display()

print " total number of students are: %d" %  student.i

Use Of Pre-defined Functions

Instead of using general statements to access attributes, programmers can use the following functions:
  • getattr( obj, name [,default] ) : used to access object’s attribute
  • hasattr( object, name) : used for checking whether the attribute exists or not
  • setattr( obj, name, value ) : set or create an attribute if doesn’t exist
  • delattr( obj, name ) : used to delete an attribute

Built-In Class Attributes

All the Python built-in class attributes can be accessed using dot (.) operator like other attributes.
The built-in class attributes are:
  • __dict__: This attribute is a dictionary that contains class’s-namespace
  • __doc__: Used for class documentation string
  • __name__: used as class-name
  • __module__: used to define module name for the class in which it is defined. In interactive mode it is __main__.
  • __bases__: An empty tuple containing the base-class

Comments

For Programs Click Here

Popular posts from this blog

Python Lists

Python Lists Dealing with data in a structured format is quiet generous, and this is possible if those data are set accordingly in a specific manner. So, Python provides these data structures named ‘lists’ and ‘tuples’ that are used to organize data in  single set. Python has 6 built-in sequences and among them the most famous are “lists and tuples”. The lists are containers that hold a number of other objects in a given order. It usually puts into practice the sequence protocol and allows programmers to add or remove objects from that sequence. Each element of the sequence is assigned a number i.e. he index and the first index is 0 (zero). This versatile data-type of Python is written in a sequence of list separated by commas between expressions. Creating Lists To build a list, just put a number of expressions in square brackets. The syntax is: Syntax: lst1 = [ ] # lst1 is the name of the list lst2 = [ expression1 , …. , expression_N ] Example: #!/usr/bin/python ls...

C++ Objects and Classes

C++ Object Oriented C++ Objects and Classes In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. What is a class? A class is an abstract data type similar to ‘ C structure ‘. Class representation of objects and the sets of operations that can be applied to such objects. Class consists of Data members and methods. Primary purpose of a class is to held data/information. This is achieved with attributes which is also know as data members. The member functions determine the behavior of the class i.e. provide definition for supporting various operations on data held...

C++ Polymorphism

C++ Polymorphism The term Polymorphism get derived from the Greek word where  poly  +  morphos  where poly means many and morphos means forms. In programming background, polymorphism can be broadly divided into two parts. These are: Static Polymorphism Dynamic Polymorphism. Polymorphism is another concept of object oriented programming (OOPs). The attitude which lies beneath this concept is “single interface having multiple implementations.” This provides a single interface for controlling access to a general class of actions. Polymorphism can be gained in both ways: compile time and run time A common and simple example of polymorphism is when you used  >>  and  << as operator overloading in C++, for  cin  and  cout  statements respectively. This bitwise shift operator at that time acts as a inclusion operator and its overloaded meaning is defined in  iostream  header file. Static Polymorphism In static polymor...