classes in python - cs.drexel.eduknowak/cs265_fall_2010/sn_pythonclasses.pdf · • you may only...

21
Classes in Python Steve Nguyen 1 Saturday, November 20, 2010

Upload: others

Post on 07-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Classes in PythonSteve Nguyen

1

Saturday, November 20, 2010

Page 2: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Overview

• Class Definitions, Objects, Instance Objects, Method Objects

• Inheritance

• Multiple Inheritance

• Private Variables

• Odds and Ends

• Exceptions

• Iterators

• Generators and Generator Expressions

2

Saturday, November 20, 2010

Page 3: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Class Definitions

The simplest form of a class definition

• Class definitions must be executed before they have any effect

• Functions definitions take the form of an argument list• Uses the same calling conventions as methods

• A new namespace is created when a class is defined and use as a local scope.

• namespace: a mapping of names to objects• scope: the region where a namespace can be accessed

directly3

Saturday, November 20, 2010

Page 4: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Class Objects

Attribute References:•Uses the standard . (dot) syntax•Example: MyClass.i or MyClass.f

•MyClass.i will return 12345•MyClass.f will return the function f

•MyClass.__doc__ is also valid, returning the docstring of the class

4

Saturday, November 20, 2010

Page 5: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Class ObjectsClass Instantiation:•Uses the same syntax as function notation

•x = MyClass()•Assigns new class instance to variable x

•Classes can define a special method, __init__()• •Creates a new class instance•Can contain arguments

5

Saturday, November 20, 2010

Page 6: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Instance Objects

• You may only use attribute references within instance objects (data attributes and methods)

• Data attributes are the same as members in C++

• Instance methods are class functions that are called within an instance

• Example: x.f is valid since it is a function defined in MyClass (see Slide 4), but x.i is not

6

Saturday, November 20, 2010

Page 7: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Method Objects

• If x.f() is an instance method, then it can be stored within another variable for later use.

• xf = x.f()

• However, the function definition in MyClass was f(self), it had an argument

• Python automatically fills this in, it is equivalent to calling MyClass.f(x)

• Uses the method’s object as the argument self

7

Saturday, November 20, 2010

Page 8: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Remarks about Attributes

• Data attributes override method attributes with the same name (case-sensitive)

• Data attributes can be referenced by methods and by users, you cannot implement pure abstract data types with classes

• The first argument of a method is self, which holds no real significance other than for readability

• Functions can be defined outside the class and then assigned to a local variable

• Methods can call other methods using the self argument

8

Saturday, November 20, 2010

Page 9: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Inheritance

9

The basic format of a derived class definition

If the base class is contained in another module

•The rules for creating a derived class are the same as a normal, base class.

•Class objects look for data attributes and methods in the derived class first, then look in the base classes

•Derived classes may still override methods of base classes

•However, derived class methods may extend base class methods by calling BaseClassName.methodname(self, arguments)

Saturday, November 20, 2010

Page 10: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Inheritance

• There are two built-in functions that work with inheritance

• isinstance(object, type) checks an instance’s type

• issubclass(class1, class2) checks if class1 is a subclass of class2

10

Saturday, November 20, 2010

Page 11: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Multiple Inheritance

11

Standard syntax for multiple base classes

•Searches for methods: Depth-first, left-to-right

•super(type[,object-or-type])•Used to access inherited methods that have been overridden by the derived class

Saturday, November 20, 2010

Page 12: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Private Variables

• Technically, there are no such things

• There is a naming convention that implies that something should be treated as non-public, _name (one underscore)

• Limited support through name mangling

• Anything in the form __name (two underscores, followed by at most one)

• Textually replaced with _classname__name

• Makes it difficult to access the variable, but not impossible

12

Saturday, November 20, 2010

Page 13: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Odds and Ends

13

Create data types by bundling together named data items and using an empty class definition.

•Can create classes can emulate the methods of a particular data type.

•A function that reads and formats data from a file can be defined as a class that uses read() and readline()

Saturday, November 20, 2010

Page 14: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Exceptions

14

The raise statement:•raise Class, instance

• instance must be an instance of the class or a class derived from it

•raise instance•shorthand for instance.__class__, instance

Unhandled exceptions print exceptionclass: instance

Saturday, November 20, 2010

Page 15: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Iterators

15

for-loops create a start iterator and uses them to traverse the elements in an array, list, file, etc

Saturday, November 20, 2010

Page 16: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Iterators

16

Saturday, November 20, 2010

Page 17: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Generators

• Generators are used to create iterators

• Use the yield statement to return data

• They are more compact forms of class-based iterators, they create __iter__() and next() automatically. Also raise StopIteration automatically.

• Written like ordinary functions

17

Saturday, November 20, 2010

Page 18: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Generatorsdef reverse(data) :

for index in range(len(data)-1, -1, -1) :

yield data[index]

>>> for char in reverse( ‘golf ’ ) :

...print char

...

18

Saturday, November 20, 2010

Page 19: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Generator Expressions

• You can create generators on the fly as expressions, using ( ) instead of [ ]

• sum(i*i for i in range(10))

• Automatically creates iterator to traverse the list created by range

19

Saturday, November 20, 2010

Page 20: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

Questions?

20

Saturday, November 20, 2010

Page 21: Classes in Python - cs.drexel.eduknowak/cs265_fall_2010/sn_PythonClasses.pdf · • You may only use attribute references within instance objects (data attributes and methods) •

References

"9. Classes — Python V2.7 Documentation." Overview — Python V2.7 Documentation. The Python Software Foundation, 13 Nov. 2010. Web. 20 Nov. 2010. <http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects>.

21

Saturday, November 20, 2010