开发者

In Python, how can you respond to method calls that don't exist on an object? [duplicate]

This question already has answers here: Closed 12 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜