在Python中将PHP的__get()与__get __()和__getattr __()比较 [英] Comparing PHP's __get() with __get__() and __getattr__() in Python

查看:78
本文介绍了在Python中将PHP的__get()与__get __()和__getattr __()比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中的__get__()__getattr__()有什么区别?我来自PHP背景,那里只有__get().什么时候应该使用哪个功能?

What is the difference between __get__() and __getattr__() in Python? I come from a PHP background, where there is only __get(). When should I use which function?

一段时间以来,我一直在试图解决这个问题.我看到很多类似这个问题的问题,询问之间的区别但是__getattr__()__getattribute__().

I've been trying to figure this out for a while. I see plenty of questions like this one, asking about the difference between __getattr__() and __getattribute__(), though.

推荐答案

您将找到所有这些方法的详细文档

You will find detailed documentation for all those methods here.

来自PHP,首先应该使自己熟悉Python对象模型.它比PHP丰富得多,因此您不应尝试将1:1的PHP知识映射到Python.如果要开发PHP,请使用PHP.如果要使用Python开发,请学习Python.

Coming from PHP, you should first make yourself familiar with the Python object model. It's much richer than PHP, so you should not try to map your PHP knowledge 1:1 to Python. If you want to develop PHP, use PHP. If you want to develop in Python, learn Python.

回到您的原始问题:__getattr__可能是与PHP中的__get函数相同的函数. Python中的__get__用于实现描述符.关于描述符的详细信息也可以在我上面提到的文档中找到.

Coming back to your original question: __getattr__ is probably the function which does the same as the __get function in PHP. __get__ in Python is used to implement descriptors. Details about descriptors can also be found in the documentation I mentioned above.

这篇关于在Python中将PHP的__get()与__get __()和__getattr __()比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆