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++ Qualifiers and Storage Classes

C++ Qualifiers and Storage Classes Qualifiers and storage class are smaller but important programming concept that helps to make the quality of a variable more accurate for using that variable within the program. In this chapter you will learn about how qualifiers are used with variables and what the roles of different storage classes in C++ are. What is a Qualifier? A qualifier is a token added to a variable which adds an extra  quality , such as specifying volatility or constant-ness to a variable. They act like an adjective for a variable. With or without a qualifier, the variable itself still occupies the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers just specify something about how it may be accessed or where it is stored. Three qualifiers are there in C++. These are: const : This is used to define that the type is constant. volatile : his is used to define that the type is volatile. mutable:  applies to non...

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...