Download presentation
Presentation is loading. Please wait.
Published byOswald Boyd Modified over 9 years ago
1
Lilian Blot TOWARDS MORE ADVANCED CONCEPTS & OBJECT ORIENTED PROGRAMMING Building Data Structure Autumn 2014 TPOP 1
2
Lilian Blot Overview Data representation Building new data structures Examples Autumn 2014 TPOP 2
3
Lilian Blot Data Representation Example: Address house number street name city county postcode Person Surname First name NI Address Phone numbers Autumn 2014 TPOP 3
4
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 2014 TPOP 4
5
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 2014 TPOP 5
6
Lilian Blot Creating our Own Data Structure Keyword class General Form: Example Autumn 2014 TPOP 6 class DataStructureName: ``` doc-string ``` pass Code class Address: ``` doc-string ``` pass Code
7
Lilian Blot Creating our Own Data Structure Using the new Data structure : Autumn 2014 TPOP 7 >>> addr = Address() >>> addr.street = ‘Lili Street’ >>> print addr.street Lili Street Code
8
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 2014 TPOP 8
9
Lilian Blot Creating our Own Data Structure Keyword class Key method __init__(…) Key parameter self isinstance(object,Class) Autumn 2014 TPOP 9 class DataStructureName: ``` doc-string ``` def __init__(self, ): Code
10
Lilian Blot Creating our Own Data Structure Code examples: First Attempt: Address Autumn 2014 TPOP 10
11
Lilian Blot Address Creating our Own Data Structure Code examples: First Attempt: Address Autumn 2014 TPOP 11 … 35 … … self number streetcitycounty postcode house_number house_street house_city house_county house_postcode my_address
12
Lilian Blot Creating our Own Data Structure Code examples: First Attempt: Address Second Attempt: Address Autumn 2014 TPOP 12
13
Lilian Blot Address Creating our Own Data Structure Code examples: First Attempt: Address Second Attempt: Address Autumn 2014 TPOP 13 … 35 … … number streetcitycounty postcode my_address 555
14
Lilian Blot Creating our Own Data Structure The objects we define are MUTABLE Autumn 2014 TPOP 14
15
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 2014 TPOP 15
16
Lilian Blot Creating our Own Data Structure Code examples: First Attempt: Address Second Attempt: Address Person Autumn 2014 TPOP 16
17
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 2014 TPOP 17
18
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 2014 TPOP 18
19
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 2014 TPOP 19
20
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 2014 TPOP 20
21
Lilian Blot Exercises Client requirements Build 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 2014 TPOP 21
22
Lilian Blot Nested Structures Autumn 2014 TPOP 22
23
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 2014 TPOP 23
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.