Python
  • Getting Started
    • Input and Output
    • Variables
    • Data Types
    • Conditional Structures
      • Humidity Level
      • Financial Transactions
      • Fruit-Vending Machine
      • Magic While
      • FizzBuzz
    • Functions
      • Hashtag Generator
  • Data Structures
    • Lists
    • Dictionary
    • Set
    • Interpreter Expressions
  • OOPs
    • Classes and Objects
    • Exception Handling
      • Simple Calculator
  • System Programming
    • File Operations
    • Directory Navigation
    • Process Creation
    • Threads
Powered by GitBook
On this page
  • What is a Class
  • Attributes
  • Methods
  • Instance attributes in python and the init method
  • Inheritance
  1. OOPs

Classes and Objects

I LOVE DOING THIS :)

All the code written in Python is implemented using a special construct called Classes

What is a Class

Basically a code template for creating Objects

Objects have member variables and have behaviour associated with them

>>> class Python:
...    pass
...
>>> python = Python()
>>> print(python)
<__main__.Python object at 0x102735060>

Attributes

The above example has a class, but within the class there are no functionalities so it's of no use. Functionalities are defined by setting Attributes, which perform certain functions or act like some containers for storing data - These functions are called Methods

>>> class Python:
...    name = "Akash"            // So, here name is an attribute

We can even assign the class to a variable - object instantiation

We can then access the attributes that are present inside the class using the dot . operator

>>> python = Python()            // Assigning the class to a variable

>>> print(python.name)          // Accessing the attribute using the dot operator
Akash

Methods

When we define methods, we'll have to always provide the first argument to the method with a self keyword

>>> class Python:
...    name = "Akash"
...    def change_name(self, new_name):
...        self.name = new_name
...
>>> python = Python()          // instantiate the class
>>> print(python.name)        // printing the current obj
Akash
>>> python.change_name("Kannan")        // changing the name using change_method
>>> print(python.name)
Kannan

Instance attributes in python and the init method

We can also provide the values for the attributes at runtime

This is done by defining the attributes inside the init method

>>> class Python:
...    def __init__(self, name):
...        self.name = name
...    def change_name(self, new_name):
...        self.name = new_name

Now we can directly define separate attribute values for separate objects

>>> python = Python("Akash")
>>> var = Python("Kannan")
>>> print(python.name)
Akash
>>> print(var.name)
Kannan

Inheritance

Inheritance is implemented, the methods and attributes that were defined in the base class will also be present in the inherited class

An object is based on another object, we can derive the base class as the following

>>> class DerivedClassName(BaseClassName):
...    pass

Here's a simple example to master Inheritance Concept, whic uses Cars as the base class and Type as the inherited class :)

#!/opt/python3

class Cars:
	def __init__(self, name, color):
		self.name = name
		self.color = color

	def intro(self):
		return "This is my car %s and it's color is %s" % (self.name, self.color)

class Benz(Cars):
	def __init__(self, name, color, speed):
		Cars.__init__(self, name, color)
		self.speed = speed

	def get_speed(self):
		return "The speed of my car %s is %d kmphs" % (self.name, self.speed)

if __name__ == "__main__":

	a = Cars(input(),input())
	b = Benz(input(),input(),int(input()))
	print(a.intro())
	print(b.intro())
	print(b.get_speed())

Output

:~# python3 classes.py
alto
red
rover
yellow
240
This is my car alto and it's color is red
This is my car rover and it's color is yellow
The speed of my car rover is 240 kmphs
PreviousInterpreter ExpressionsNextException Handling

Last updated 2 years ago