Python
Python TUPLE - Pack, Unpack, Compare, Slicing, Delete, Key
What is Tuple Matching in Python? Tuple Matching in Python is a method of grouping the tuples by...
OOPs in Python is a programming approach that focuses on using objects and classes as same as other general programming languages. The objects can be any real-world entities. Python allows developers to develop applications using the OOPs approach with the major focus on code reusability. It is very easy to create classes and objects in Python.
A Class in Python is a logical grouping of data and functions. It gives the freedom to create data structures that contains arbitrary content and hence easily accessible.
For example, for any bank employee who want to fetch the customer details online would go to customer class, where all its attributes like transaction details, withdrawal and deposit details, outstanding debt, etc. would be listed out.
In this tutorial, we will learn,
To define class you need to consider following points
Step 1) In Python, classes are defined by the "Class" keyword
class myClass():
Step 2) Inside classes, you can define functions or methods that are part of this class
def method1 (self): print "gtupapers" def method2 (self,someString): print "Software Testing:" + someString
Step 3) Everything in a class is indented, just like the code in the function, loop, if statement, etc. Anything not indented is not in the class
NOTE: About using "self" in Python
Step 4) To make an object of the class
c = myClass()
Step 5) To call a method in a class
c.method1()
c.method2(" Testing is fun")
Step 6) Here is the complete code
# Example file for working with classes
class myClass():
def method1(self):
print("gtupapers")
def method2(self,someString):
print("Software Testing:" + someString)
def main():
# exercise the class methods
c = myClass ()
c.method1()
c.method2(" Testing is fun")
if __name__== "__main__":
main()Inheritance is a feature used in object-oriented programming; it refers to defining a new class with less or no modification to an existing class. The new class is called derived class and from one which it inherits is called the base. Python supports inheritance; it also supports multiple inheritances. A class can inherit attributes and behavior methods from another class called subclass or heir class.
Python Inheritance Syntax
class DerivedClass(BaseClass):
body_of_derived_class
Step 1) Run the following code
# Example file for working with classes
class myClass():
def method1(self):
print("gtupapers")
class childClass(myClass):
#def method1(self):
#myClass.method1(self);
#print ("childClass Method1")
def method2(self):
print("childClass method2")
def main():
# exercise the class methods
c2 = childClass()
c2.method1()
#c2.method2()
if __name__== "__main__":
main()
Notice that the in childClass, method1 is not defined but it is derived from the parent myClass. The output is "gtupapers."
Step 2) Uncomment Line # 8 & 10. Run the code
Now, the method 1 is defined in the childClass and output "childClass Method1" is correctly shown.
Step 3) Uncomment Line #9. Run the code
You can call a method of the parent class using the syntax
ParentClassName.MethodName(self)
In our case, we call, myClass.method1(self) and gtupapers is printed as expected
Step 4) Uncomment Line #19. Run the code.
Method 2 of the child class is called and "childClass method2" is printed as expected.
A constructor is a class function that instantiates an object to predefined values.
It begins with a double underscore (_). It __init__() method
In below example we are taking name of the user using constructor.
class User:
name = ""
def __init__(self, name):
self.name = name
def sayHello(self):
print("Welcome to gtupapers, " + self.name)
User1 = User("Alex")
User1.sayHello()
Output will be:
Welcome to gtupapers, Alex
Python 2 Example
Above codes are Python 3 examples, If you want to run in Python 2 please consider following code.
# How to define Python classes
# Example file for working with classes
class myClass():
def method1(self):
print "gtupapers"
def method2(self,someString):
print "Software Testing:" + someString
def main():
# exercise the class methods
c = myClass ()
c.method1()
c.method2(" Testing is fun")
if __name__== "__main__":
main()
#How Inheritance works
# Example file for working with classes
class myClass():
def method1(self):
print "gtupapers"
class childClass(myClass):
#def method1(self):
#myClass.method1(self);
#print "childClass Method1"
def method2(self):
print "childClass method2"
def main():
# exercise the class methods
c2 = childClass()
c2.method1()
#c2.method2()
if __name__== "__main__":
main()
"Class" is a logical grouping of functions and data. Python class provides all the standard features of Object Oriented Programming.
What is Tuple Matching in Python? Tuple Matching in Python is a method of grouping the tuples by...
What is a Dictionary in Python? A Dictionary in Python is the unordered and changeable collection of...
What is Python Range? Python range() is a built-in function available with Python from...
What is Regular Expression in Python? A Regular Expression (RE) in a programming language is a...
Python is one of the most popular programming languages. Currently, each of the following six...
What is Python strip()? Python strip() function is a part of built-in functions available in the...