81 lines
2.6 KiB
Plaintext
81 lines
2.6 KiB
Plaintext
:link-with-uscores1: https://docs.python.org/3/reference/datamodel.html#object.__getitem__
|
|
:link-with-uscores2: https://docs.python.org/3/reference/datamodel.html#object.__class_getitem__
|
|
:link-with-uscores3: https://docs.python.org/3/reference/datamodel.html#object.__setitem__
|
|
:link-with-uscores4: https://docs.python.org/3/reference/datamodel.html#object.__delitem__
|
|
|
|
Getting, setting and deleting items using square brackets requires the accessed object to have special methods:
|
|
|
|
* Getting items such as ``++my_variable[key]++`` requires ``++my_variable++`` to have the {link-with-uscores1}[``++__getitem__++``] method, or the {link-with-uscores2}[``++__class_getitem__++``] method if ``++my_variable++`` is a class.
|
|
* Setting items such as ``++my_variable[key] = 42++`` requires ``++my_variable++`` to have the {link-with-uscores3}[``++__setitem__++``] method.
|
|
* Deleting items such as ``++del my_variable[key]++`` requires ``++my_variable++`` to have the {link-with-uscores4}[``++__delitem__++``] method.
|
|
|
|
This rule raises an issue when an item operation is performed on an object which doesn't have the corresponding method.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
del (1, 2)[0] # Noncompliant, tuples are immutable
|
|
(1, 2)[0] = 42 # Noncompliant
|
|
(1, 2)[0]
|
|
|
|
class A:
|
|
def __init__(self, values):
|
|
self._values = values
|
|
|
|
a = A([0,1,2])
|
|
|
|
a[0] # Noncompliant
|
|
del a[0] # Noncompliant
|
|
a[0] = 42 # Noncompliant
|
|
|
|
class B:
|
|
pass
|
|
|
|
B[0] # Noncompliant
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
del [1, 2][0] # Lists are mutable
|
|
[1, 2][0] = 42
|
|
[1, 2][0]
|
|
|
|
class A:
|
|
def __init__(self, values):
|
|
self._values = values
|
|
|
|
def __getitem__(self, key):
|
|
return self._values[key]
|
|
|
|
def __setitem__(self, key, value):
|
|
self._values[key] = value
|
|
|
|
def __delitem__(self, key):
|
|
del self._values[key]
|
|
|
|
a = A([0,1,2])
|
|
|
|
a[0]
|
|
del a[0]
|
|
a[0] = 42
|
|
|
|
class B:
|
|
def __class_getitem__(cls, key):
|
|
return [0, 1, 2, 3][key]
|
|
|
|
B[0]
|
|
----
|
|
|
|
:link-with-uscores1: https://docs.python.org/3/reference/datamodel.html#object.__getitem__
|
|
:link-with-uscores2: https://docs.python.org/3/reference/datamodel.html#object.__setitem__
|
|
:link-with-uscores3: https://docs.python.org/3/reference/datamodel.html#object.__delitem__
|
|
:link-with-uscores4: https://docs.python.org/3/reference/datamodel.html#object.__class_getitem__
|
|
|
|
== See
|
|
|
|
* {link-with-uscores1}[Python documentation - ++__getitem__++ method]
|
|
* {link-with-uscores2}[Python documentation - ++__setitem__++ method]
|
|
* {link-with-uscores3}[Python documentation - ++__delitem__++ method]
|
|
* {link-with-uscores4}[Python documentation - ++__class_getitem__++ method]
|