56 lines
1.2 KiB
Plaintext
56 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
A class without an explicit extension of object (``++class ClassName(object)++``) is considered an old-style class, and ``++__slots__++`` declarations are ignored in old-style classes. Having such a declaration in an old-style class could be confusing for maintainers and lead them to make false assumptions about the class.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
class A:
|
|
__slots__ = ["id"] # Noncompliant; this is ignored
|
|
|
|
def __init__(self):
|
|
self.id = id
|
|
self.name = "name" # name wasn't declared in __slots__ but there's no error
|
|
|
|
a = A()
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
class A(object): # Converting to a new-style class is the preferred method of addressing this issue
|
|
__slots__ = ["id"]
|
|
|
|
def __init__(self):
|
|
self.id = id
|
|
self.name = "name" # "name" is not listed in __slots__, so as expected there is an error in this line
|
|
|
|
a = A()
|
|
----
|
|
or
|
|
|
|
[source,python]
|
|
----
|
|
class A:
|
|
|
|
def __init__(self):
|
|
self.id = id
|
|
self.name = "name"
|
|
|
|
a = A()
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|