Day-14 Python Class
Day-14 Python Class
Day-14 Python Class
A Class 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.
class myClass():
Step 2) Inside classes, you can define functions or methods that are part of this
class
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
c = myClass()
c.method1()
c.method2(" Testing is fun")
Notice that when we call the method1 or method2, we don't have to supply
the self-keyword. That's automatically handled for us by the Python runtime.
Python runtime will pass "self" value when you call an instance method on in
instance, whether you provide it deliberately or not
You just have to care about the non-self arguments
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()
The self Parameter
The self parameter is a reference to the class itself, and is used to access
variables that belongs to the class.
It does not have to be named self , you can call it whatever you like, but it has
to be the first parameter of any function in the class:
Example
Use the words mysillyobject and abc instead of self:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Example
Set the age of p1 to 40:
p1.age = 40
Example
Delete the age property from the p1 object:
del p1.age
Delete Objects
You can delete objects by using the del keyword:
Example
Delete the p1 object:
del p1
How Inheritance works
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.
class DerivedClass(BaseClass):
body_of_derived_class
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 "Guru99."
Now, the method 1 is defined in the childClass and output "childClass Method1" is
correctly shown.
ParentClassName.MethodName(self)
Python Constructors
A constructor is a class function that instantiates an object to predefined values.
def sayHello(self):
print("Welcome to Guru99, " + self.name)
User1 = User("Alex")
User1.sayHello()
Python 2 Example
Above codes are Python 3 examples, If you want to run in Python 2 please consider
following code.
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()
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()
Summary:
"Class" is a logical grouping of functions and data. Python class provides all the
standard features of Object Oriented Programming.