【问题标题】:How to access to an specific attribute of an object in a python class?如何访问python类中对象的特定属性?
【发布时间】:2018-12-12 19:13:37
【问题描述】:

您好,我正在尝试构建一个类来清理字符串,但是我不明白为什么会得到以下输出:

 python3 clean.py 
<bound method clean_string.split_func of <__main__.clean_string object at 0x7fb70486b0f0>>

我的班级如下所示:

class clean_string:

    def __init__(self,cadena):
        self.replace_chars = {"á":"a","ó":"o"}
        self.cadena = cadena
    def split_func(self):
        return self.cadena.split(' ')

test_string = clean_string('this is a test')

但是,当我执行代码时,我只得到了内存引用对象:

print(test_string.split_func)
<bound method clean_string.split_func of <__main__.clean_string object at 0x7fb70486b0f0>>

我想得到以下输出:

['this', 'is', 'a', 'test']

【问题讨论】:

    标签: python-3.x class methods reference


    【解决方案1】:

    print(test_string.split_func)

    这一行打印什么是 split_func,它是一个函数。如果要打印函数返回的内容,则需要执行 print(test_string.split_func())

    没有最后的括号,它会打印 split_func 是什么,这就是你看到的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多