AttributeError:“module”对象没有属性“something”

2024-09-30 22:17:22 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试做一些类似的事情:

facility = 'LOG_LOCAL7'
syslog.openlog( 'Blah', 0, syslog.facility)

但我得到:

^{pr2}$

我想在一个变量中设置设施。。。在

另外,在Python中,这个语句的不同部分实际调用了什么?在

syslog.openlog( 'Blah', 0, syslog.LOG_LOCAL7)
  • 系统日志
  • 开放日志
  • “废话”

是类、方法、属性吗?在


Tags: 方法log属性语句事情blahsyslog系统日志
2条回答

要回答第一部分,您需要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'(函数的一个参数)

您想使用getattr(https://docs.python.org/3/library/functions.html#getattr

facility = 'LOG_LOCAL7'
syslog.openlog( 'Blah', 0, getattr(syslog, facility))

相关问题 更多 >