Upload
oswald-boyd
View
212
Download
0
Embed Size (px)
Citation preview
Lilian BlotLilian Blot
TOWARDS MORE ADVANCED CONCEPTS&
OBJECT ORIENTED PROGRAMMING
Building Data Structure
Autumn 2014TPOP
1
Lilian Blot
Overview
Data representation
Building new data structures
Examples
Autumn 2014TPOP
2
Lilian Blot
Data Representation
Example: Address
house number street name city county postcode
Person Surname First name NI Address Phone numbers
Autumn 2014TPOP
3
Lilian Blot
Using what we know so far
Lists Address[house_number, street_name, city, county, postcode] Person[surname, first_name, NI, address, phone_numbers]
String Address“house_number, street_name, city, county, postcode” Person“surname, first_name, NI, address, phone_numbers”
Autumn 2014TPOP
4
Lilian Blot
Using what we know so far
Dictionary Address{house_number:15, street_name : ‘Lili Street’, city: ‘York’, county: ‘Yorkshire’, postcode: ‘YO5-5GH’}
Person{surname: ‘Blot’, first_name: ‘Lilian’, NI: ‘OO7’, address:…, phone_numbers: {…} }
Autumn 2014TPOP
5
Lilian Blot
Creating our Own Data Structure
Keyword class General Form:
Example
Autumn 2014TPOP
6
class DataStructureName:``` doc-string ```pass
Code
class Address:``` doc-string ```pass
Code
Lilian Blot
Creating our Own Data Structure
Using the new Data structure:
Autumn 2014TPOP
7
>>> addr = Address()>>> addr.street = ‘Lili Street’>>> print addr.streetLili Street
Code
Lilian Blot
What are Classes?
Classes are composed from structural and behavioural constituents.
Attributes (member variables or instance variables) enable a class instance to maintain state. a.k.a properties, fields, data members
Methods, enable the behaviour of class instances.
Classes define the type of their instances
Autumn 2014TPOP
8
Lilian Blot
Creating our Own Data Structure
Keyword class Key method __init__(…)Key parameter self
isinstance(object ,Class)
Autumn 2014TPOP
9
class DataStructureName:``` doc-string ```def __init__(self, <parameters>):
<body>
Code
Lilian Blot
Creating our Own Data Structure
Code examples:
First Attempt: Address
Autumn 2014TPOP
10
Lilian Blot
Address
Creating our Own Data Structure
Code examples:
First Attempt: Address
Autumn 2014TPOP
11
…
35
…
…
…
self
number
street
city
county
postcode
house_number
house_street
house_city
house_county
house_postcode
my_address
Lilian Blot
Creating our Own Data Structure
Code examples:
First Attempt: Address
Second Attempt: Address
Autumn 2014TPOP
12
Lilian Blot
Address
Creating our Own Data Structure
Code examples:
First Attempt: Address
Second Attempt: Address
Autumn 2014TPOP
13
…
35
…
…
…
number
street
city
county
postcode
my_address
555
Lilian Blot
Creating our Own Data Structure
The objects we define are
MUTABLE
Autumn 2014TPOP
14
Lilian Blot
Terminology
Class Address is the definition of an object
my_address is an instance of Address
number, city, postcode,… are attributes
__init__ is a constructor
__str__ is a method
Autumn 2014TPOP
15
Lilian Blot
Creating our Own Data Structure
Code examples:
First Attempt: Address
Second Attempt: Address
Person
Autumn 2014TPOP
16
Lilian Blot
__repr__ Method
Definition: Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.
Autumn 2014TPOP
17
Lilian Blot
__str__ Method
Definition :Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
Autumn 2014TPOP
18
Lilian Blot
__str__ versus __repr__
Every class should implement the __repr__ method
__str__ is optional. If not implemented, __repr__ will be used instead
__repr__: representation of python object usually eval will convert it back to that object
__str__: is whatever you think is that object in text form
Autumn 2014TPOP
19
Lilian Blot
Summary
We have seen how to build our own data structure
Our data structure can be embedded into another data structure
User defined Objects are MUTABLE
Autumn 2014TPOP
20
Lilian Blot
Exercises
Client requirementsBuild a genealogy tree
First Name, Surname, Maiden name Date of Birth Place of Birth (city/town/village and country) Current Address
Your Job:Build an appropriate data structure
Autumn 2014TPOP
21
Lilian Blot
Nested Structures
Autumn 2014TPOP
22
Lilian Blot
Exercises
Food for thought
how to save/read the data structure into/from a text file
how to search for/find a person in the tree
how to add/remove a person from the tree
could you implement your solution?
Autumn 2014TPOP
23