mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-14 09:34:46 +00:00
Update classmethod with reviewer comments
This commit is contained in:
parent
e5f866626e
commit
2e3d29de45
@ -64,13 +64,13 @@ TEST_SUBMODULE(class_, m) {
|
|||||||
py::class_<NoConstructor>(m, "NoConstructor")
|
py::class_<NoConstructor>(m, "NoConstructor")
|
||||||
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance")
|
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance")
|
||||||
.def_classmethod(
|
.def_classmethod(
|
||||||
"new_instance_uuid",
|
"new_instance_seq_id",
|
||||||
[](py::object &cls) {
|
[](py::type &cls) {
|
||||||
py::int_ uuid = getattr(cls, "uuid", py::int_(0));
|
py::int_ seq_id = getattr(cls, "seq_id", py::int_(0));
|
||||||
cls.attr("uuid") = uuid + py::int_(1);
|
cls.attr("seq_id") = seq_id + py::int_(1);
|
||||||
return NoConstructorNew::new_instance();
|
return NoConstructorNew::new_instance();
|
||||||
},
|
},
|
||||||
"Returns a new instance and then increment the uuid");
|
"Returns a new instance and then increment the seq_id");
|
||||||
|
|
||||||
py::class_<NoConstructorNew>(m, "NoConstructorNew")
|
py::class_<NoConstructorNew>(m, "NoConstructorNew")
|
||||||
.def(py::init([](const NoConstructorNew &self) { return self; })) // Need a NOOP __init__
|
.def(py::init([](const NoConstructorNew &self) { return self; })) // Need a NOOP __init__
|
||||||
|
@ -32,9 +32,10 @@ def test_instance_new(msg):
|
|||||||
|
|
||||||
|
|
||||||
def test_classmethod(num_instances=10):
|
def test_classmethod(num_instances=10):
|
||||||
|
assert not hasattr(m.NoConstructor, "seq_id")
|
||||||
for i in range(num_instances):
|
for i in range(num_instances):
|
||||||
assert getattr(m.NoConstructor, "uuid", 0) == i
|
m.NoConstructor.new_instance_seq_id()
|
||||||
m.NoConstructor.new_instance_uuid()
|
assert m.NoConstructor.seq_id == i + 1
|
||||||
|
|
||||||
|
|
||||||
def test_type():
|
def test_type():
|
||||||
|
Loading…
Reference in New Issue
Block a user