FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/python/ql/src/Classes/EqualsOrHash.qhelp at codeql-cli/v2.19.2 · github/codeql · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
github
/
codeql
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10k
Code
Issues
997
Pull requests
467
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql
/
python
/
ql
/
src
/
Classes
/
EqualsOrHash.qhelp
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (34 loc) · 1.77 KB
Breadcrumbs
codeql
/
python
/
ql
/
src
/
Classes
/
EqualsOrHash.qhelp
Copy path
File metadata and controls
46 lines (34 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
40
41
42
43
44
45
46
<!
DOCTYPE
qhelp
PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<
qhelp
>
<
overview
>
<
p
>In order to conform to the object model, classes that define their own equality method should also
define their own hash method, or be unhashable. If the hash method is not defined then the <
code
>hash</
code
> of the
super class is used. This is unlikely to result in the expected behavior.</
p
>
<
p
>A class can be made unhashable by setting its <
code
>__hash__</
code
> attribute to <
code
>None</
code
>.</
p
>
<
p
>In Python 3, if you define a class-level equality method and omit a <
code
>__hash__</
code
> method
then the class is automatically marked as unhashable.</
p
>
</
overview
>
<
recommendation
>
<
p
>When you define an <
code
>__eq__</
code
> method for a class, remember to implement a <
code
>__hash__</
code
> method or set
<
code
>__hash__ = None</
code
>.</
p
>
</
recommendation
>
<
example
>
<
p
>In the following example the <
code
>Point</
code
> class defines an equality method but
no hash method. If hash is called on this class then the hash method defined for <
code
>object</
code
>
is used. This is unlikely to give the required behavior. The <
code
>PointUpdated</
code
> class
is better as it defines both an equality and a hash method.
If <
code
>Point</
code
> was not to be used in <
code
>dict</
code
>s or <
code
>set</
code
>s, then it could be defined as
<
code
>UnhashablePoint</
code
> below.
</
p
>
<
p
>
To comply fully with the object model this class should also define an inequality method (identified
by a separate rule).</
p
>
<
sample
src
=
"
EqualsOrHash.py
"
/>
</
example
>
<
references
>
<
li
>Python Language Reference: <
a
href
=
"
http://docs.python.org/reference/datamodel.html#object.__hash__
"
>object.__hash__</
a
>.</
li
>
<
li
>Python Glossary: <
a
href
=
"
http://docs.python.org/2/glossary.html#term-hashable
"
>hashable</
a
>.</
li
>
</
references
>
</
qhelp
>
Back
|
FazBrowse Home
|
New Git URL