在构造函数中设置字典(python)
嘿,我是一个Python菜鸟,我试图在类的构造函数中设置一个字典,但是,每当我尝试访问一个元素时,它都会返回 None 。这是我的代码
class GoogleApp:
def __init__(self, path):
self.name = "Google App"
self.description = "Creates an new google app project."
self.title = None
self.path = path
self.pre_defined_macros = {
'_NAME': self.name ,
'_DESCRIPTION': self.description,
'_TITLE': "Shoutout",
'_BASE_PATH': self.path,
'_PATH': lambda: os.path.join(self.path, self.title)
}
现在,每当我尝试从其他方法访问 self.pre_define_macros['_TITLE'] 时,它都会返回 None 。
Hey I am a python rookie and I am trying to setup a dict in the constructor of a class how ever, whenever I try to access an element, it returns None. Here is my code
class GoogleApp:
def __init__(self, path):
self.name = "Google App"
self.description = "Creates an new google app project."
self.title = None
self.path = path
self.pre_defined_macros = {
'_NAME': self.name ,
'_DESCRIPTION': self.description,
'_TITLE': "Shoutout",
'_BASE_PATH': self.path,
'_PATH': lambda: os.path.join(self.path, self.title)
}
Now whenever I try to access self.pre_defined_macros['_TITLE'] from some other method it returns None.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以发布示例代码吗?
我问这个是因为我不确定你想做什么。
.pre_define_macros['_TITLE']
在我测试时工作正常。我还尝试从另一种方法访问
pre_defined_macros
。就像这样:Can you post sample code?
I ask this because I'm not sure what you are trying to do.
<instance>.pre_defined_macros['_TITLE']
worked fine when I tested it.I also tried accessing
pre_defined_macros
from another method. Like so:啊啊……明白了!
实际上我已经设置了
所以在创建字典时
None 是 '_TITLE' 的值
缺乏注意力!
Aah ... Got it!
Actually I've set
So while creating the dictionary
None is the value for '_TITLE'
Lack of concentration !!