【问题标题】:How to get current instance in class?如何在课堂上获取当前实例?
【发布时间】:2015-04-17 05:32:01
【问题描述】:

我不知道如何在课堂上获取当前实例。

class Basic(object):
    def __init__(self, val):
        self.val = val

    def current_ins(self):
        ???

有没有办法在 python 中使用val 获取当前实例? 谢谢。

【问题讨论】:

  • self 是当前实例
  • 我多么愚蠢。谢谢,它解决了!

标签: python class object instance


【解决方案1】:

“当前实例”是self

【讨论】:

    【解决方案2】:

    Self 是所有类的当前实例。在你的情况下,

    class Basic(object):
        def __init__(self, val):
            self.val = val
    
        def current_ins(self):
            #???
            print self.val    # one can use self to access all the member of it's class.
    

    【讨论】:

      猜你喜欢
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2020-03-06
      • 2022-09-23
      相关资源
      最近更新 更多