Você está na página 1de 19

Class

data has various types so objects can have


different types
collections of objects with identical
characteristics are collectively known as a
class
A class is just a user-defined data type.
An instance is just an object that belongs to a
class.

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

data type that


can handle
integer, string
and list

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(.)

objectname.attributename =another object


Creating
another
attributes by
assignment
Jagsel Biosolutions

Class attributes
class attribute - defined in the class, either

textually in a class definition or later by


assignment to an attribute reference of the
class andstored in the class's name space
Ex:
Class foo:
foo.a=1
Class
attribute
a & b=Class
attribute
foo.b=1

Jagsel Biosolutions

Continue

Jagsel Biosolutions

Instance attribute
instance attribute - defined in the instance, by

assignment, and is stored in the instance's


name space.
Ex:
class foo:
a=1
i = foo()
a=instance
attribute
i.a => 1

Jagsel Biosolutions

Instance attributes

Jagsel Biosolutions

Defining methods for a


class

Jagsel Biosolutions

Constructor method

i.e., setting initial


values to the
class

The 1st step after the class is invoked is

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

already defined class and extend it without


affecting the code
We can extend subclasses(Inheritance)
Have a parent and child relationship.

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

Você também pode gostar