在构造函数中设置字典(python)

发布于 2024-10-20 05:11:35 字数 574 浏览 3 评论 0原文

嘿,我是一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-10-27 05:11:35

您可以发布示例代码吗?

我问这个是因为我不确定你想做什么。 .pre_define_macros['_TITLE'] 在我测试时工作正常。

>>> 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)
    }


>>> f  = GoogleApp('foo')
>>> f.pre_defined_macros['_TITLE']
'Shoutout'
>>> 

我还尝试从另一种方法访问 pre_defined_macros 。就像这样:

>>> class GoogleApp:
    def __init__(self, path):
        # Your original code here.
    def get_title(self):
        return self.pre_defined_macros['_TITLE']


>>> f  = GoogleApp('foo')
>>> f.get_title()
'Shoutout'
>>> 

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.

>>> 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)
    }


>>> f  = GoogleApp('foo')
>>> f.pre_defined_macros['_TITLE']
'Shoutout'
>>> 

I also tried accessing pre_defined_macros from another method. Like so:

>>> class GoogleApp:
    def __init__(self, path):
        # Your original code here.
    def get_title(self):
        return self.pre_defined_macros['_TITLE']


>>> f  = GoogleApp('foo')
>>> f.get_title()
'Shoutout'
>>> 
如果没有你 2024-10-27 05:11:35

啊啊……明白了!

实际上我已经设置了

self.title = None

所以在创建字典时

'_TITLE': self.title

None 是 '_TITLE' 的值

缺乏注意力!

Aah ... Got it!

Actually I've set

self.title = None

So while creating the dictionary

'_TITLE': self.title

None is the value for '_TITLE'

Lack of concentration !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文