Also add a note about inheritance from `object` being default.
##
-class Foo(object):
+class Foo:
def f(self):
print('you called Foo.f()')
def g(self):
import sqlite3
-class Point(object):
+class Point:
def __init__(self, x, y):
self.x, self.y = x, y
import sqlite3
-class Point(object):
+class Point:
def __init__(self, x, y):
self.x, self.y = x, y
import sqlite3
-class Point(object):
+class Point:
def __init__(self, x, y):
self.x, self.y = x, y
that is normally used. This can be achieved by specifying the ``namespace=``
keyword argument::
- >>> class C(object):
+ >>> class C:
... pass
...
>>> c = C()
:attr:`_as_parameter_` attribute and uses this as the function argument. Of
course, it must be one of integer, string, or bytes::
- >>> class Bottles(object):
+ >>> class Bottles:
... def __init__(self, number):
... self._as_parameter_ = number
...
['Struct', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
'unpack', 'unpack_from']
- >>> class Foo(object):
+ >>> class Foo:
... def __dir__(self):
... return ["kan", "ga", "roo"]
...
function for setting, and *fdel* a function for del'ing, an attribute. Typical
use is to define a managed attribute ``x``::
- class C(object):
+ class C:
def __init__(self):
self._x = None
property will copy *fget*'s docstring (if it exists). This makes it possible to
create read-only properties easily using :func:`property` as a :term:`decorator`::
- class Parrot(object):
+ class Parrot:
def __init__(self):
self._voltage = 100000
corresponding accessor function set to the decorated function. This is
best explained with an example::
- class C(object):
+ class C:
def __init__(self):
self._x = None
attribute. For example, the following two statements create identical
:class:`type` objects:
- >>> class X(object):
+ >>> class X:
... a = 1
...
>>> X = type('X', (object,), dict(a=1))
code execution::
# example code for resolving the builtin descriptor types
- class _foo(object):
+ class _foo:
__slots__ = ['foo']
slot_descriptor = type(_foo.foo)
:func:`groupby` is equivalent to::
- class groupby(object):
+ class groupby:
# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
# [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
def __init__(self, iterable, key=None):
from multiprocessing.managers import BaseManager
- class MathsClass(object):
+ class MathsClass:
def add(self, x, y):
return x + y
def mul(self, x, y):
This is a good approach if you write the class yourself. Let's suppose you have
a class like this::
- class Point(object):
+ class Point:
def __init__(self, x, y):
self.x, self.y = x, y
A class definition is an executable statement. The inheritance list usually
gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so
each item in the list should evaluate to a class object which allows
-subclassing.
+subclassing. Classes without an inheritance list inherit, by default, from the
+base class :class:`object`; hence, ::
+
+ class Foo:
+ pass
+
+is equivalent to ::
+
+ class Foo(object):
+ pass
The class's suite is then executed in a new execution frame (see :ref:`naming`),
using a newly created local namespace and the original global namespace.
dictionary. That behaviour is the reason why the following code raises an
exception::
- >>> class C(object):
+ >>> class C:
... pass
...
>>> c = C()