Escolar Documentos
Profissional Documentos
Cultura Documentos
Jagsel Biosolutions
Defining Classes
Python class is created by a class definition
Syntax:
class name[(expr[,expr]*)]:
type
builtin data
suite
Note: The convention is to use initial capital
letters for the names of classes.
Jagsel Biosolutions
Is it similar to structure?
Classes -similar to records of Pascal and
structs of C
Ex:
Class foo:
a, b, c = 0, Gene", (1,2)
Jagsel Biosolutions
Instantiating classes
Classes (like functions) are callable
If call them they return an instance
A class is instantiated by calling the class
object:
i = foo()
print i.a, i.b, i.c
Jagsel Biosolutions
i=Instance of
the class foo
Example
Jagsel Biosolutions
Attributes
an attribute is a way to get from one object to
another
objectname.attributename
Now point
to
another
object
using
dot(.)
Class attributes
class attribute - defined in the class, either
Jagsel Biosolutions
Continue
Jagsel Biosolutions
Instance attribute
instance attribute - defined in the instance, by
Jagsel Biosolutions
Instance attributes
Jagsel Biosolutions
Jagsel Biosolutions
Constructor method
instantiation process.
__init__(class constructor) called during
instantiating process and defines the object
Jagsel Biosolutions
__init__ method
Jagsel Biosolutions
Example
Jagsel Biosolutions
Error
Jagsel Biosolutions
Creating subclassInheritance
Powerful aspects of OOP-ability to take an
Jagsel Biosolutions
Example
Jagsel Biosolutions
Conclusion
Python is fully object-oriented
We can define our own classes
inherit from our own or built-in classes
instantiate the classes we've defined
Jagsel Biosolutions