Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.

Similar presentations


Presentation on theme: "Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined."— Presentation transcript:

1 Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined classes / objects Class Object Python’s self parameter Overrideable methods __init__ and __str__ Accessor and mutator methods (“Getter” and “Setter” methods) Class diagrams in UML

2 Classes are everywhere …

3 Python programs A Python program is a sequence of Python statements in one or more files When a Python program is executing, virtually everything in a Python program is an object Each object has a type Program output :

4 Object and Class Object’s type = Class used to create the object Object can also be referred to as an instance of a class.

5 Each object has … A type (class that created it) Identity (location in computer memory) Data attributes (variables in a class) and/or methods (functions in a class) Special data attributes and/or methods prefixed and suffixed with a double underscore (e.g. __doc__)

6 Variables and objects Variables are names associated with objects The object's data attributes and methods can be accessed using the dot operator with the name of the data attribute or method dot operator

7 Special data attributes and methods Data attributes (e.g. __doc__, __class__, etc) Methods (e.g. __add__, __str__, etc) NOTE: Good Ref: http://www.rafekettler.com/magicmethods.htmlhttp://www.rafekettler.com/magicmethods.html

8 Mutable and Immutable objects If object data attributes can be changed, the object is mutable If object data attributes cannot be changed, the object is immutable

9 One object with many names Assignment of variables to the same values means the variables will be associated with the same objects (same identity and type) Output:

10 Variables with immutable and mutable types Assigning variables of immutable types to another and then changing either value creates a new object Assigning variables of mutable types to one another and then changing either value will modify the value of both mutableimmutable

11 The is operator and isinstance function The is operator checks to see if the identity of the operands is the same The isinstance(object, class | type | tuple) built-in function returns True or False

12 To get a list of standard Python classes, etc. help('__builtin__') provides list of built-in classes, functions, and data

13 Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined classes / objects Class Object Python’s self parameter Overrideable methods __init__ and __str__ Accessor and mutator methods (“Getter” and “Setter” methods) Class diagrams in UML

14 Proc vs OOP Procedural ProgrammingObject Oriented Programming (OOP) Good for simple programs with simple flows and models Good for complex programs with complex models and interactions. Not good for modelling real-world objects and interactions. Overkill for simple programs.

15 Entity representation Procedural ProgrammingObject Oriented Programming (OOP) Entities represented by groups of variables Entities represented by abstractions of real-world objects Abstraction captures only those details about an object that are relevant to the current perspective.

16 Entity representation Procedural ProgrammingObject Oriented Programming (OOP) Entity data represented by variablesEntity data represented by data attributes defined in the class Abstraction captures only those details about an object that are relevant to the current perspective.

17 Methods Procedural ProgrammingObject Oriented Programming (OOP) Operations represented by functions available at module level Operations represented by object methods available at object level Object properties and methods are encapsulated by the object instance.

18 Program flow Procedural ProgrammingObject Oriented Programming (OOP) Program flow controlled by order of setting variables, calling functions, and passing data between functions. Procedural-OOP Hybrid : Program flow controlled by order of creating objects, calling its methods, and setting its properties. OOP : Program flow controlled by messages sent between objects and/or events raised by an object, code that responds to those events.

19 OOP Concepts - Class A class is a template for an object. The class defines the data attributes (properties) and methods (functions in a class) that will be common to all objects created from it. Create user-defined classes using class keyword.

20 OOP Concepts - Object Instance of a class created when the class definition is executed. Every instance of the class will have the same attributes -- properties and methods -- but can have different data (property values)

21 Python’s (self) parameter It is a parameter that gets assigned “automatically” to the object being processed. “self” is the first, and sometimes only, parameter in methods Name “self” is Python convention. Could be anything.

22 __init__ ( self [,params] ) It is the first method run when the class is called. The initialization method or constructor. Actually, __new__ is called first but it is rarely used and it passes everything along to __init__. Technically, __new__ and __init__ is the constructor. Most classes override__init__.

23 __str__( self ) Another of many methods that are common to all classes in Python that you can override (replace default behaviour with your own) Others include: __len__ __add__ __eq__ __lt__ etc.

24 Accessor (“getter”) method Control getting property values (object data) Without accessor: __private With accessor:

25 Mutator (“setter”) method Control setting property values (object data)

26 New (2.6+) approach to properties Old approach New approach Calls to getter functions (@property)

27 Class Diagram UML Graphical display of the contents of a class Dice color : str value : int roll() : None Class name Properties (data attributes) Methods Key

28 Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined classes / objects Class Object Python’s self parameter Overrideable methods __init__ and __str__ Accessor and mutator methods (“Getter” and “Setter” methods) Class diagrams in UML


Download ppt "Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined."

Similar presentations


Ads by Google