AttributeError: 'module' object has no attribute 'something'
AttributeError: 'module' object has no attribute 'something'
尝试做类似的事情:
facility = 'LOG_LOCAL7'
syslog.openlog( 'Blah', 0, syslog.facility)
但我得到:
AttributeError: 'module' object has no attribute 'facility'
我希望能够在变量中设置设施...
旁注;在 Python 中,该语句的不同部分实际上称为什么?
syslog.openlog( 'Blah', 0, syslog.LOG_LOCAL7)
- 系统日志
- 打开日志
- 'Blah'
是class、方法、属性吗?
您想使用 getattr (https://docs.python.org/3/library/functions.html#getattr)
facility = 'LOG_LOCAL7'
syslog.openlog( 'Blah', 0, getattr(syslog, facility))
要回答第一部分,您需要 getattr
:
Help on built-in function getattr
in module __builtin__
:
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
所以你需要:
syslog.openlog('Blah', 0, getattr(syslog, facility))
对于你问题的第二部分,关于陈述本身的分解:
syslog
(模块)
.
(scope resolution operator)
openlog
(模块的一个属性,在本例中,它是一个函数)
'Blah'
(函数的参数)
尝试做类似的事情:
facility = 'LOG_LOCAL7'
syslog.openlog( 'Blah', 0, syslog.facility)
但我得到:
AttributeError: 'module' object has no attribute 'facility'
我希望能够在变量中设置设施...
旁注;在 Python 中,该语句的不同部分实际上称为什么?
syslog.openlog( 'Blah', 0, syslog.LOG_LOCAL7)
- 系统日志
- 打开日志
- 'Blah'
是class、方法、属性吗?
您想使用 getattr (https://docs.python.org/3/library/functions.html#getattr)
facility = 'LOG_LOCAL7'
syslog.openlog( 'Blah', 0, getattr(syslog, facility))
要回答第一部分,您需要 getattr
:
Help on built-in function
getattr
in module__builtin__
:
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case.
所以你需要:
syslog.openlog('Blah', 0, getattr(syslog, facility))
对于你问题的第二部分,关于陈述本身的分解:
syslog
(模块).
(scope resolution operator)openlog
(模块的一个属性,在本例中,它是一个函数)'Blah'
(函数的参数)