:orphan: .. _notes_session09: #################### Notes for Session 09 #################### A collection of notes to go over in class, to keep things organized. Due Date/Time ============= All submissions' due dates/times are now set to 6pm, Monday, 5/14/2018. Letter grading will start at that time with all the submissions made by that time. If you miss this deadline and haven't achieved 80% of the total possible score, you'll be given an I (incomplete) grade and a 2-week extension. We'll notify you if we have to give you an I grade and ask you to follow up. Lightning Talks =============== Anyone missed their chance? Issues that came up during the week. ==================================== "private" attributes and dunders -------------------------------- ``_something`` vs ``__something`` vs ``__something__`` Let's talk about that... See the following articles: https://www.geeksforgeeks.org/underscore-_-python/ http://igorsobreira.com/2010/09/16/difference-between-one-underline-and-two-underlines-in-python.html Adding parameters to a subclass __init__ ---------------------------------------- In general, when you override a method in a subclass, you want the method signature to be the same. That is -- all the parameters should be the same. However, sometimes, particularly with ``__init__``, you may need a couple extra parameters. To keep things clean and extensible, you want to put the extra parameters at the beginning, before the super class' parameters: And this lets you use ``*args`` and ``**kwargs`` to pass along the usual ones. .. code-block:: python class Base: def __init__(self, par1, par2, par3=something, par4=something): ... class Subclass(Base): def __init__(self, newpar1, newpar2, *args, **kwargs): self.newpar1 = newpar1 self.newpar2 = newpar2 super().__init__(*args, **kwarg) **Example:** html_render Anchor tag: .. code-block:: python class A(OneLineTag): """ anchor element """ tag = "a" def __init__(self, link, *args, **kwargs): kwargs['href'] = link super().__init__(*args, **kwargs) This becomes particularly important with ``super()`` and subclassing... Which we will get more into today. Any other html_render questions? -------------------------------- Lightning Talks =============== Anyone missed? New Topics ========== sorting ------- maybe it's a good idea to add a sort_key method to your classes? see ``IntroPython-2017/examples/Session09/sort_key.py`` let's try it on Circle... (or on Fraction ?) classmethod ----------- ``classmethod`` is really pretty simple to use, not much to talk about. But it can be a bit challenging to "get". The key point is that classmethods work for subclasses -- like for alternate constructors. Let's look at that with my Circle solution: ``IntroPython-2017/solutions/Session08/circle.py`` https://github.com/UWPCE-PythonCert-ClassRepos/Wi2018-Classroom/blob/master/solutions/Session08/circle.py#L26 (and answer any other questions about Circle, while we are at it) multiple inheritance and super() -------------------------------- ``super()`` is a mixed bag --it's actually a pretty complex topic, but can be pretty easy to use -- at least in the easy cases. To get the hang of multiple inheritance, mix-ins, and ``super()``, we'll play around with object canvas: See: ``IntroPython-2017/examples/Session09/object_canvas.py`` Object Oriented Mailroom ------------------------ One more time! Yes, it's time to make mailroom Object oriented: :ref:`exercise_mailroom_oo`