SCRUMble ! Hello Blog Readers! Thank you for all your support and encouragement. I have something exciting for you all. I have recently written and published a new book called 'SCRUMble !'. It is currently available on pothi store. It will be soon available on Amazon and Flipkart as well. Please get your copy and do let me know your reviews. -Abhishek Sathe SCRUMble ! Written and Published by: Abhishek Sathe Distributed by: pothi.com Order your copy now: https://store.pothi.com/book/abhishek-sathe-scrumble/ Coming soon on Amazon and Flipkart About the book: Scrum is a framework for solving complex problems largely adapted by Software Development field. There are multiple ag...
This post is in continuation with the previous post: Python 101.
Classes in Python:
A class in python is specified with keyword 'class'. Below is an example showing how to create a class:
class Greet:
Message = ""
def __init__(self, Name="Abhishek"):
self.Message = Name
def wish(self):
print("Good Day to {0}".format(self.Message))
MyInstance = Greet()
MyInstance.wish()
Output:
Good Day to Abhishek
Objects:
Variables and methods encapsulated together creates an object. In other words, an object is an instance of a class. In above example, 'MyInstance' is an object of class 'Greet'.
Class Attributes:
Class attributes can be accessed using a dot. In above example, we accessed a class attribute 'Message' inside the constructors as:
self.Message = Name
Constructors in Python:
__init__() is called constructor in python. The first argument the constructor takes is used to refer to instance object. Additional parameters follow this, if any. In above example, 'self' is the first parameter used to refer to the instance to which the constructor is going to initialize. Name="Abhishek" is an additional parameter.
Methods in Classes:
The functions defined inside a class are called class methods. In above example, __init__() is a constructor. There is one more method wish() defined inside class Greet.
Inheritance in Python:
The syntax to inherit one class from another is:
class Derived(BaseClass):
#code for Derived
Here class 'Derived' inherits from class 'BaseClass'.
Overriding in Python:
A class can inherit from its parent class and decide to have its own implementation. This is called overriding. Here is an example:
class Parent(object):
def greet(self):
print("Good Morning")
class Child(Parent):
def greet(self):
print("Good Night")
Destructors in python:
Similar to a constructor, we can write a destructor in pythin using keyword '__del__'. Once you are done with objects of a class, you can invoke its destructor using keyword 'del'. Here is an example:
class Demo:
def __init__(self):
print("This is constructor")
def __del__(self):
print("This is destructor")
obj = Demo()
del obj
Output:
This is constructor
This is destructor
Classes in Python:
A class in python is specified with keyword 'class'. Below is an example showing how to create a class:
class Greet:
Message = ""
def __init__(self, Name="Abhishek"):
self.Message = Name
def wish(self):
print("Good Day to {0}".format(self.Message))
MyInstance = Greet()
MyInstance.wish()
Output:
Good Day to Abhishek
Objects:
Variables and methods encapsulated together creates an object. In other words, an object is an instance of a class. In above example, 'MyInstance' is an object of class 'Greet'.
Class Attributes:
Class attributes can be accessed using a dot. In above example, we accessed a class attribute 'Message' inside the constructors as:
self.Message = Name
Constructors in Python:
__init__() is called constructor in python. The first argument the constructor takes is used to refer to instance object. Additional parameters follow this, if any. In above example, 'self' is the first parameter used to refer to the instance to which the constructor is going to initialize. Name="Abhishek" is an additional parameter.
Methods in Classes:
The functions defined inside a class are called class methods. In above example, __init__() is a constructor. There is one more method wish() defined inside class Greet.
Inheritance in Python:
The syntax to inherit one class from another is:
class Derived(BaseClass):
#code for Derived
Here class 'Derived' inherits from class 'BaseClass'.
Overriding in Python:
A class can inherit from its parent class and decide to have its own implementation. This is called overriding. Here is an example:
class Parent(object):
def greet(self):
print("Good Morning")
class Child(Parent):
def greet(self):
print("Good Night")
Destructors in python:
Similar to a constructor, we can write a destructor in pythin using keyword '__del__'. Once you are done with objects of a class, you can invoke its destructor using keyword 'del'. Here is an example:
class Demo:
def __init__(self):
print("This is constructor")
def __del__(self):
print("This is destructor")
obj = Demo()
del obj
Output:
This is constructor
This is destructor
Comments
Post a Comment