-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
should trait_names
return iterator or list? [P3 Compat]
#240
Comments
I think the current behavior - generator on Python 3, list on Python 2 makes the most sense to me. I don't think dict_keys would prevent it from being garbage collected unless someone is holding on to a reference to it, so that's probably what should be investigated, rather than using a different iterable object. |
In Py3 creating a from sys import getrefcount
d = {}
i = getrefcount(d)
k = d.keys()
f = getrefcount(d)
not_str = '' if i<f else '!'
msg = 'initial(%s) %s< final(%s)'
print(msg % (i, not_str, f)) |
Yes, but only as long as a reference is held to the iterator, because it is ultimately a reference to the dictionary itself. This would be true of any iterator that's not referring to a copy of the data. |
I just felt it made more sense to copy the keys and return an iterator so the dict (which is never used) could be released. And not that it changes functionality, but I imagine it would be surprising for someone to find that the objection returned by In any case I'm just nitpicking now since I'm no longer waiting on this. |
An implementation without def trait_names(self, **metadata):
for n in dir(self.__class__):
v = getattr(self.__class__, n)
if isinstance(v, TraitType):
for meta_name, meta_eval in metadata.values():
if type(meta_eval) is not types.FunctionType:
meta_eval = _SimpleTest(meta_eval)
if not meta_eval(v.metadata.get(meta_name, None)):
break
else:
yield n |
If a list would be simpler, I think it's fine for it to be a list. |
If it were a list, and didn't depend on |
Depending on traits is fine, so you can do |
HasTraits.trait_names
andHasTraits.class_own_trait_names
return adict_keys
iterator in Python 3, but alist
in Python 2. Should this remain true, or should we be returning lists in both versions?As a side note, returning a
dict_keys
iterator prevents the actual dict object it refers to (which is never made available to the user) from being garbage collected. So if we want to keep iterators, that might be something to consider changing.The text was updated successfully, but these errors were encountered: