Skip to content

Commit 52cce3f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 1c4f73b commit 52cce3f

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

docs/advanced/classes.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,27 +1370,29 @@ One just need to rely on the `casting capabilities`_ of `pybind11`:
13701370
# test.py
13711371
from test import Base
13721372
1373+
13731374
class Derived(Base):
13741375
def __init__(self):
13751376
Base.__init__(self)
13761377
self.foo = "hello"
13771378
1379+
13781380
b = Derived()
13791381
assert b.get_foo() == "hello"
13801382
assert not hasattr(b, "bar")
13811383
13821384
b.set_bar()
1383-
assert b.bar == 10.
1385+
assert b.bar == 10.0
13841386
13851387
13861388
However, there is a special case where such a behavior needs a hint to work as expected:
13871389
when the C++ constructor is called, the C++ object is not yet allocated and registered,
13881390
making impossible the casting operation.
13891391

1390-
It's thus impossible to access the Python object from the C++ constructor one *as is*.
1392+
It's thus impossible to access the Python object from the C++ constructor one *as is*.
13911393

1392-
Adding the ``py::preallocate()`` extra option to a constructor binding definition informs
1393-
`pybind11` to allocate the memory and register the object location just before calling the C++
1394+
Adding the ``py::preallocate()`` extra option to a constructor binding definition informs
1395+
`pybind11` to allocate the memory and register the object location just before calling the C++
13941396
constructor, enabling the use of ``py::cast(this)``:
13951397

13961398

@@ -1417,12 +1419,14 @@ constructor, enabling the use of ``py::cast(this)``:
14171419
# test.py
14181420
from test import Base
14191421
1422+
14201423
class Derived(Base):
14211424
...
14221425
1426+
14231427
b = Derived()
14241428
assert hasattr(b, "bar")
1425-
assert b.bar == 10.
1429+
assert b.bar == 10.0
14261430
14271431
14281432
.. _casting capabilities: https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html?highlight=cast#casting-back-and-forth

tests/test_methods_and_attributes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ class Derived2(m.InitPyFromCpp2):
554554
a = m.InitPyFromCppDynamic1()
555555
with pytest.raises(AttributeError):
556556
a.bar
557-
558-
# works fine
557+
558+
# works fine
559559
assert m.InitPyFromCppDynamic2().bar == 10.0
560560

561561
# 4. on derived class of base supporting dynamic attributes
@@ -567,8 +567,8 @@ class DynamicDerived1(m.InitPyFromCppDynamic1):
567567
with pytest.raises(AttributeError):
568568
d.bar
569569

570-
# works fine
570+
# works fine
571571
class DynamicDerived2(m.InitPyFromCppDynamic2):
572572
...
573573

574-
assert DynamicDerived2().bar == 10.0
574+
assert DynamicDerived2().bar == 10.0

0 commit comments

Comments
 (0)