Skip to content

Add support for write only properties #1142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TD22057 opened this issue Oct 12, 2017 · 2 comments
Closed

Add support for write only properties #1142

TD22057 opened this issue Oct 12, 2017 · 2 comments

Comments

@TD22057
Copy link
Contributor

TD22057 commented Oct 12, 2017

Issue description

We have a few cases where we have C++ code that has write only properties (just a set method) that we'd like to promote. These are basically identical to def_property_readonly except with just a set method. The current def_property can't be used because it relies on the read function always being there to get docs. The additions we made are:

  • def_property_writeonly
  • def_property_writeonly_static

If you don't think this is appropriate, feel free to close this. Otherwise, I'll submit a PR w/ the updated functions and test cases that we've implemented. If you think it's necessary, I'm sure we could also add def_writeonly and def_writeonly_static for completeness.

@jagerman
Copy link
Member

It seems fine to me; you can do something like this in pure Python via:

Python 3.5.4 (default, Sep  5 2017, 18:32:10) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C():
...     def set_var(self, v):
...         self._var = v
...     var = property(None, set_var)
... 
>>> c = C()
>>> c.var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: unreadable attribute
>>> c.var = 123
>>> 

so it'd be nice in a PR to duplicate that unreadable attribute error, but aside from that it seems a reasonable feature.

jagerman pushed a commit that referenced this issue Nov 7, 2017
py::class_<T>'s `def_property` and `def_property_static` can now take a
`nullptr` as the getter to allow a write-only property to be established
(mirroring Python's `property()` built-in when `None` is given for the
getter).

This also updates properties to use the new nullptr constructor internally.
@jagerman
Copy link
Member

jagerman commented Nov 7, 2017

Fixed via #1144.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants