In Python, how can you respond to method calls that don't exist on an object? [duplicate]
Possible Duplicate:
Dynamic Finders and Method Missing in Python
I know you can do this in Ruby, but how would it be done in Python?
So basically you could maybe to a regex on the 开发者_运维问答method call that didn't exist, and create 'syntactic sugar'.
Define __getattr__
.
But that still won't make s./.*/
valid syntax, if that's what you have in mind. Python's syntax is nowhere as bendable as Ruby's.
You can override __getattribute__
on an object to handle requests for attributes that do not exist. This only works for new-style classes. If you have old-style classes, you can use delnans answer.
精彩评论