【python基础 - 04】__getattr__

本文探讨了Python中的__getattr__方法,它允许以属性方式访问类中的属性或方法。通过示例展示了如何使用__getattr__来实现动态调用类的元素,以及在实际应用中的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

__getattr__方法常用于以属性的方式调用类中的属性或方法,如MyClass.my_func().

定义Test类如下:

class Test(object):
    def _my_default(self, *args):
        print(f'func input {
     args
``` soho@ubuntu:~/Downloads/RE$ pip install python-lzo Collecting python-lzo Downloading https://files.pythonhosted.org/packages/a4/8f/60cbc1a57257cd21c9e35217a8bc8713424dc7502df17794d914738df50f/python-lzo-1.15.tar.gz Building wheels for collected packages: python-lzo Running setup.py bdist_wheel for python-lzo ... error Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-MK08ju/python-lzo/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpyGIvKCpip-wheel- --python-tag cp27: running bdist_wheel running build running build_ext building 'lzo' extension creating build creating build/temp.linux-x86_64-2.7 x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-D7NSzq/python2.7-2.7.17=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include -I/usr/include/python2.7 -c lzomodule.c -o build/temp.linux-x86_64-2.7/lzomodule.o lzomodule.c:38:10: fatal error: lzo/lzo1.h: No such file or directory #include <lzo/lzo1.h> ^~~~~~~~~~~~ compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ---------------------------------------- Failed building wheel for python-lzo Running setup.py clean for python-lzo Failed to build python-lzo Installing collected packages: python-lzo Running setup.py install for python-lzo ... error Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-MK08ju/python-lzo/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-_zspTr-record/install-record.txt --single-version-externally-managed --compile --user --prefix=: running install running build running build_ext building 'lzo' extension creating build creating build/temp.linux-x86_64-2.7 x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-D7NSzq/python2.7-2.7.17=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include -I/usr/include/python2.7 -c lzomodule.c -o build/temp.linux-x86_64-2.7/lzomodule.o lzomodule.c:38:10: fatal error: lzo/lzo1.h: No such file or directory #include <lzo/lzo1.h> ^~~~~~~~~~~~ compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-MK08ju/python-lzo/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-_zspTr-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-MK08ju/python-lzo/```分析报错
最新发布
04-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雁宇up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值