S
Sean DiZazzo
Should the following be legal?
'123'
~Sean
'123'
~Sean
Should the following be legal?
'123'
Good question, great answer!Terry said:Should the following be legal?
'123'
Different people have different opinions as to whether setattr (and
correspondingly getattr) should be strict or permissive as to whether or
not the 'name' string is a legal name. CPython is permissive. The
rationale is that checking would take time and prevent possible
legitimate use cases.
CPython is actually looser than this. Try
t.__dict__[1] = 2
Now there is an 'attribute' whose 'name' is an int! -- and which can
only be accessed via the same trick of delving into the internals. This
is, however, implementation behavior that would go away if an
implementation used string-key-only dicts to store attributes.
Should the following be legal?
'123'
Different people have different opinions as to whether setattr (and
correspondingly getattr) should be strict or permissive as to whether or
not the 'name' string is a legal name. CPython is permissive. The
rationale is that checking would take time and prevent possible
legitimate use cases.
CPython is actually looser than this. Try
t.__dict__[1] = 2
Now there is an 'attribute' whose 'name' is an int! -- and which can
only be accessed via the same trick of delving into the internals. This
is, however, implementation behavior that would go away if an
implementation used string-key-only dicts to store attributes.
Terry Jan Reedy
Should the following be legal?class TEST(object): pass
...
t = TEST()
setattr(t, "", "123")
getattr(t, "")
'123'
Different people have different opinions as to whether setattr (and
correspondingly getattr) should be strict or permissive as to whether or
not the 'name' string is a legal name. CPython is permissive. The
rationale is that checking would take time and prevent possible
legitimate use cases.
CPython is actually looser than this. Try
t.__dict__[1] = 2
Now there is an 'attribute' whose 'name' is an int! -- and which can
only be accessed via the same trick of delving into the internals. This
is, however, implementation behavior that would go away if an
implementation used string-key-only dicts to store attributes.
Terry Jan Reedy
Interesting. I can understand the "would take time" argument, but I
don't see any legitimate use case for an attribute only accessible via
getattr(). Well, at least not a pythonic use case.
Interesting. I can understand the "would take time" argument, but I
don't see any legitimate use case for an attribute only accessible via
getattr(). Well, at least not a pythonic use case.
Lie Ryan said:mostly for people (ab)using attributes instead of dictionary.
Here is one use case:
A query application. Queries are described by complex query objects.
For efficiency reasons, query results should be cached. For this, it is
not unnatural to use query objects as cache keys. Then, query objects
must not get changed in an uncontrolled way. I use "__setattr__" to
control access to the objects.
Steven D'Aprano said:Here is one use case:
A query application. Queries are described by complex query objects.
For efficiency reasons, query results should be cached. For this, it is
not unnatural to use query objects as cache keys. Then, query objects
must not get changed in an uncontrolled way. I use "__setattr__" to
control access to the objects.
(1) Wouldn't it be more natural to store these query keys in a list or
dictionary rather than as attributes on an object?
e.g. instead of:
cache.__setattr__('complex query object', value)
use:
cache['complex query object'] = value
(2) How does __setattr__ let you control access to the object? If a user
wants to modify the cache, and they know the complex query object, what's
stopping them from using __setattr__ too?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.