classes and modules
TRANSCRIPT
-
8/20/2019 Classes and Modules
1/19
-
8/20/2019 Classes and Modules
2/19
Classdata has various types so objects can havediferent types
collections o objects with identical
characteristics are collectively known as aclass
A class is just a user-dened data type.
An instance is just an object that belongs to aclass.
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
3/19
Defning ClassesPython class is created by a class denition
Synta!
class name[ "expr[ #expr]$% ]! builtin datatype
suite
&ote! The convention is to use initial capitalletters for the names of classes.
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
4/19
's it si(ilar to structure)Classes -si(ilar to records o Pascal and
structs o C
*!
Class oo! a# b# c + ,# ene/# "0#1% data type that
can handleinteger# string
and list
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
5/19
'nstantiating classesClasses "like unctions% are callable
' call the( they return an instance
A class is instantiated by calling the class
object!i + oo"%
print i.a# i.b# i.c
i+'nstance othe class oo
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
6/19
*a(ple
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
7/19
Attributes
an attribute is a way to get ro( one object toanother
objectna(e.attributena(e
objectna(e.attributena(e +another object
&ow point
toanotherobjectusingdot".%
Creatinganother
attributes byassign(ent
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
8/19
Class attributes
class attribute - dened in the class# eithertetually in a class denition or later byassign(ent to an attribute reerence o theclass andstored in the class2s na(e space
*!
Class oo!
oo.a+0 Class attribute oo.b+0
a 3 b+Classattribute
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
9/19
Continue4
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
10/19
'nstance attributeinstance attribute - dened in the instance# by
assign(ent# and is stored in the instance2sna(e space.
*!class oo!
a + 0
i + oo"%
i.a => 0a+instanceattribute
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
11/19
'nstance attributes
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
12/19
5ening (ethods or aclass
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
13/19
Constructor (ethod 6he 0st step ater the class is invoked is
instantiation process.
77init77"class constructor% called duringinstantiating process and denes the object
i.e.# setting initial
values to theclass
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
14/19
77init77 (ethod
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
15/19
*a(ple
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
16/19
*rror
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
17/19
Creating subclass-'nheritancePowerul aspects o 88P-ability to take an
already dened class and etend it withoutafecting the code
9e can etend :subclasses"'nheritance%
;ave a
-
8/20/2019 Classes and Modules
18/19
*a(ple
Jagsel Biosolutions
-
8/20/2019 Classes and Modules
19/19
ConclusionPython is ully object-oriented
9e can dene our own classes
inherit ro( our own or built-in classes instantiate the classes we2ve dened
Jagsel Biosolutions