csca08 f all 2017
play

CSCA08 F ALL 2017 W EEK 8 - I NTRO TO OOP Brian Harrington & - PowerPoint PPT Presentation

Admin Object-Orientation Objects Self Break Private Variables init & str CSCA08 F ALL 2017 W EEK 8 - I NTRO TO OOP Brian Harrington & Marzieh Ahmadzadeh University of Toronto Scarborough October 30 - November 3, 2017 Admin


  1. Admin Object-Orientation Objects Self Break Private Variables init & str CSCA08 F ALL 2017 W EEK 8 - I NTRO TO OOP Brian Harrington & Marzieh Ahmadzadeh University of Toronto Scarborough October 30 - November 3, 2017

  2. Admin Object-Orientation Objects Self Break Private Variables init & str A DMIN • TT2 • Details on course website • Covers everything up to and including dictionaries (week 7 lecture, week 8 tutorials + inverted) • Beware the code mangler

  3. Admin Object-Orientation Objects Self Break Private Variables init & str D EFINING OUR OWN T YPES • We’ve seen a bunch of types so far ( int , float , list , dict ...). • Now it’s time to define our own

  4. Admin Object-Orientation Objects Self Break Private Variables init & str O BJECT O RIENTATION : A N EW P ARADIGM • Until now: Functions were the focus • my_function(data) : One global function that gets data passed to it • Object Oriented Approach: • my_obect.method(data) : The object has its own methods and data

  5. Admin Object-Orientation Objects Self Break Private Variables init & str S OME TERMINOLOGY • Class: The type of an object • Object: An instance of a class • Method: Like a function, but belongs to a class • We’ve already seen this: • my_string = str(12.57) • my_string.ljust(10) • my_string is an object , of the str class, and we called string’s ljust method on it

  6. Admin Object-Orientation Objects Self Break Private Variables init & str C REATING O BJECTS • Create a class: class ClassName(): • (We’ll see what the brackets are for later) • CamelCase (not pot_hole_case) • define a method def method_name(self) • Looks very similar to defining a function • Indented inside the class • We’ll see what the self does in a minute • can now create a new object of type ClassName • my_object = ClassName() • our new object can now access the methods we defined • my_object.method_name()

  7. Admin Object-Orientation Objects Self Break Private Variables init & str S ELF • Every method (including the built in ones) gets implicitly passed a copy of the object upon which it was called • We don’t include it in the method call, but we do in the method definition • This allows a method to access the object on which it was called • We normally call this copy of our object self • Behind the scenes: my_obj.method() is really just an alias of Class.method(my_obj)

  8. Admin Object-Orientation Objects Self Break Private Variables init & str B REAK WARNING: Scary Halloween Image Ahead

  9. Admin Object-Orientation Objects Self Break Private Variables init & str B REAK

  10. Admin Object-Orientation Objects Self Break Private Variables init & str W HAT ’ S W ITH ALL THE U NDERSCORES ? • Already saw, we can do: my_object.variable • This is bad. Why? • External code relying on internals of class • We should be able to change internals without worrying about breaking external code • Like the difference between internal/external documentation • This will be a major topic in A48 • using underscores = people unlikely to guess variable names • Really: Just a way of saying “Hands off my variables" • Doesn’t actually stop external code, if they know the variable name, they can still access/change it. • Security through obscurity/convention

  11. Admin Object-Orientation Objects Self Break Private Variables init & str B UILT - IN M ETHODS • __init__ • Initialize: this is known as a constructor method • Defines the code that runs when we first create a new object of this type • Usually used to set up the default parameters • __str__ • Return what you want to output when an object of this class is cast to a string (or printed)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend