Skip to content

Commit d356c72

Browse files
committed
cleaned up html_render solution a bit.
1 parent f216a5c commit d356c72

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

materials/source/notes/session09.rst

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,49 @@ Matthew D Briggs
2222
Issues that came up during the week.
2323
====================================
2424

25-
"private" attributes and dunders...
25+
"private" attributes and dunders
26+
--------------------------------
27+
28+
``_something`` vs ``__something`` vs ``__something__``
29+
30+
Adding parameters to a subclass __init__
31+
----------------------------------------
32+
33+
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.
34+
35+
36+
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:
37+
38+
And this lets you use ``*args`` and ``**kwargs`` to pass along the usual ones.
39+
40+
.. code-block:: python
41+
42+
class Base:
43+
def __init__(self, par1, par2, par3=something, par4=something):
44+
...
45+
46+
class Subclass(Base):
47+
def __init__(self, newpar1, newpar2, *args, **kwargs):
48+
self.newpar1 = newpar1
49+
self.newpar2 = newpar2
50+
super().__init__(*args, **kwarg)
51+
52+
Example: html_render Anchor tag:
53+
54+
.. code-block:: python
55+
56+
class A(OneLineTag):
57+
"""
58+
anchor element
59+
"""
60+
tag = "a"
61+
62+
def __init__(self, link, *args, **kwargs):
63+
kwargs['href'] = link
64+
super().__init__(*args, **kwargs)
65+
66+
2667
27-
_ vs __
2868
2969
3070

solutions/Session07/step_5/html_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Title(OneLineTag):
103103

104104
class SelfClosingTag(Element):
105105
"""
106-
Base class for tags that have no content
106+
base class for tags that have no content
107107
"""
108108

109109
def append(self, *args, **kwargs):

solutions/Session07/step_6/html_render.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ class A(OneLineTag):
140140
"""
141141
tag = "a"
142142

143-
def __init__(self, link, content, **kwargs):
143+
def __init__(self, link, *args, **kwargs):
144144
kwargs['href'] = link
145-
super().__init__(content, **kwargs)
145+
super().__init__(*args, **kwargs)
146146
# this could also be direct:
147-
# Element.__init__(self, content, **kwargs)
147+
# Element.__init__(self, *args, **kwargs)
148148

solutions/Session07/step_7/html_render.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ class A(OneLineTag):
139139
"""
140140
tag = "a"
141141

142-
def __init__(self, link, content, **kwargs):
142+
def __init__(self, link, *args, **kwargs):
143143
kwargs['href'] = link
144-
super().__init__(content, **kwargs)
144+
super().__init__(*args, **kwargs)
145145
# this could also be direct:
146-
# Element.__init__(self, content, **kwargs)
146+
# Element.__init__(self, *args, **kwargs)
147147

148148

149149
class Ul(Element):
@@ -166,6 +166,6 @@ class H(OneLineTag):
166166
"""
167167
tag = "H"
168168

169-
def __init__(self, level, content=None, **kwargs):
169+
def __init__(self, level, *args, **kwargs):
170170
self.tag = "h" + str(int(level))
171-
super().__init__(content, **kwargs)
171+
super().__init__(*args, **kwargs)

solutions/Session07/step_8/html_render.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def append(self, *args, **kwargs):
118118
raise TypeError("You can not add content to a self closing tag")
119119

120120
def render(self, out_file, ind=""):
121-
# there is some repition here -- maybe factor that out?
121+
# there is some repetition here -- maybe factor that out?
122122
open_tag, _ = self.make_tags()
123-
# make it a self cloding tag by adding the /
123+
# make it a self closing tag by adding the /
124124
out_file.write(ind + open_tag.replace(">", " />"))
125125

126126

@@ -144,11 +144,11 @@ class A(OneLineTag):
144144
"""
145145
tag = "a"
146146

147-
def __init__(self, link, content, **kwargs):
147+
def __init__(self, link, *args, **kwargs):
148148
kwargs['href'] = link
149-
super().__init__(content, **kwargs)
149+
super().__init__(*args, **kwargs)
150150
# this could also be direct:
151-
# Element.__init__(self, content, **kwargs)
151+
# Element.__init__(self, *args, **kwargs)
152152

153153

154154
class Ul(Element):
@@ -171,9 +171,9 @@ class H(OneLineTag):
171171
"""
172172
tag = "H"
173173

174-
def __init__(self, level, content=None, **kwargs):
174+
def __init__(self, level, *args, **kwargs):
175175
self.tag = "h" + str(int(level))
176-
super().__init__(content, **kwargs)
176+
super().__init__(*args, **kwargs)
177177

178178

179179
class Meta(SelfClosingTag):

0 commit comments

Comments
 (0)