-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSubclassShadowing.qhelp
More file actions
39 lines (31 loc) · 1.39 KB
/
SubclassShadowing.qhelp
File metadata and controls
39 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
When an object has an attribute that shares its name with a method on the object's class (or another class attribute), the instance attribute is
prioritized during attribute lookup, shadowing the method.
If a method on a subclass is shadowed by an attribute on a superclass in this way, this may lead to unexpected results or errors, as this
shadowing behavior is nonlocal and may be unintended.
</p>
</overview>
<recommendation>
<p>
Ensure method names on subclasses don't conflict with attribute names on superclasses, and rename one.
If the shadowing behavior is intended, ensure this is explicit in the superclass.
</p>
</recommendation>
<example>
<p>
In the following example, the <code>_foo</code> attribute of class <code>A</code> shadows the method <code>_foo</code> of class <code>B</code>.
Calls to <code>B()._foo()</code> will result in a <code>TypeError</code>, as <code>3</code> will be called instead.
</p>
<sample src="examples/SubclassShadowingBad.py" />
<p>
In the following example, the behavior of the <code>default</code> attribute being shadowed to allow for customization during initialization is
intended in within the superclass <code>A</code>. Overriding <code>default</code> in the subclass <code>B</code> is then OK.
</p>
<sample src="examples/SubclassShadowingGood.py" />
</example>
</qhelp>