Skip to content

Commit 63577f0

Browse files
Add extra example
1 parent 2516f94 commit 63577f0

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

python/ql/src/Classes/SubclassShadowing/SubclassShadowing.qhelp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ In the following example, the <code>_foo</code> attribute of class <code>A</code
2626
Calls to <code>B()._foo()</code> will result in a <code>TypeError</code>, as <code>3</code> will be called instead.
2727
</p>
2828

29-
<sample src="examples/SubclassShadowingGood.py" />
29+
<sample src="examples/SubclassShadowingBad.py" />
3030

3131
<p>
32-
In the following example...
32+
In the following example, the behavior of the <code>default</code> attribute being shadowed to allow for customization during initialization is
33+
intended in within the superclass <code>A</code>. Overriding <code>default</code> in the subclass <code>B</code> is then OK.
3334
</p>
3435

36+
<sample src="examples/SubclassShadowingGood.py" />
3537

3638
</example>
3739
</qhelp>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class A:
2+
def __init__(self):
3+
self._foo = 3
4+
5+
class B:
6+
# BAD: _foo is shadowed by attribute A._foo
7+
def _foo(self):
8+
return 2
9+
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
class A:
2-
def __init__(self):
3-
self._foo = 3
4-
5-
class B:
6-
# BAD: _foo is shadowed by attribute A._foo
7-
def _foo(self):
8-
return 2
2+
def __init__(self, default_func=None):
3+
if default_func is not None:
4+
self.default = default_func
95

6+
# GOOD: The shadowing behavior is explicitly intended in the superclass.
7+
def default(self):
8+
return []
9+
10+
class B(A):
11+
12+
# Subclasses may override the method `default`, which will still be shadowed by the attribute `default` if it is set.
13+
# As this is part of the expected behavior of the superclass, this is fine.
14+
def default(self):
15+
return {}

0 commit comments

Comments
 (0)