I'm trying to create a method on my class which will return only public properties as a dict. I am struggling with the syntax. This is what I tried unsuccessfully:
@attr.s
class C:
x = attr.ib()
_y = attr.ib()
def _no_privates(a, _):
return not a.name.startswith("_")
def public_properties_to_dict(self):
return attr.asdict(self, filter=attr.filters.include(_no_privates))
In you example, _no_privates
is not function but a method.
The solution which I consider better would be to move _no_privates
out of the class into the top scope. It's a function that takes an argument and returns a value based on that argument and nothing else.
If you insist on making _no_privates
a method, you can write filter=attr.filters.include(self._no_privates)
.
However the signature of _not_privates
is wrong in any case. If it's a function, it should only take a
. If it's a method, the first argument is self
, followed by a
.