【问题标题】:Still not able to print class with __str__仍然无法使用 __str__ 打印课程
【发布时间】:2019-04-21 23:34:53
【问题描述】:
class student(object):
 def __init__(self, grade, clas):
  self.grade=grade
  self.clas=clas

def __str__(self):
 return "test"

mark=student("f","freshman")
print(mark)

这是结果 ma​​in.student 对象位于 0xb33eb2d0>

在安卓上通过termux学习python

【问题讨论】:

  • 检查__str__的缩进,它必须在class student
  • 感觉自己像个白痴。就是这样。习惯为我缩进,我的坏

标签: android string termux


【解决方案1】:

发件人: Python_syntax_and_semantics#Indentation

Python 使用空格来分隔控制流块(遵循越位规则)。 Python 从其前身 ABC 中借用了这一特性:它使用缩进来指示块的运行,而不是标点符号或关键字。

所以你的代码应该是:

class student(object):
  def __init__(self, grade, clas):
    self.grade=grade
    self.clas=clas
  def __str__(self):
    return "test"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-08
    • 2014-08-04
    • 1970-01-01
    • 2014-09-04
    • 2021-09-11
    • 2013-04-18
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多