【问题标题】:class and methods in python. introduction in ooppython中的类和方法。 oop 中的介绍
【发布时间】:2023-03-15 16:46:02
【问题描述】:
class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(myname):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(myname)

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())

它假设产生 “你好,我叫马克。”或“嗨,我叫史蒂夫。”

但它会产生 “嗨,我的名字未定义。”

【问题讨论】:

  • 为什么一定要讨厌缩进?
  • @mrdomoboto 真的不需要老兄。只需让他缩进他的代码即可。
  • 对不起。我不明白。缩进哪一个?
  • @Mr.Python 当您要求其他人用依赖空格作为范围的语言来帮助您解决问题时,这是基本的礼貌。
  • 我完全同意。不过没必要这么说

标签: python class oop object methods


【解决方案1】:

它应该在内存中打印对象的表示(类似于Hi, my name is <__main__.Person object at 0x005CEA10>)。

原因是方法的第一个参数应该是调用该方法的对象。

就像您拥有def __init__(self, name): 一样,您应该拥有def introduction(self, myname):

然后你会遇到另一个问题,因为introduction 现在需要一个你没有提供的参数myname。但是,现在不需要它,因为您可以访问 self.myname

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)


# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())

会输出

Hi, my name is Mark.
Hi, my name is Steve.

【讨论】:

    【解决方案2】:

    您需要将introduction() -> introduction(self) 声明为实例方法(通过传入 self)才能访问实例变量self.myname

    class Person:
    
        def __init__(self, name):
            """Make a new person with the given name."""
            self.myname = name
    
        def introduction(self):
            """Returns an introduction for this person."""
            return "Hi, my name is {}.".format(self.myname)
    

    示例输出:

    # Use the class to introduce Mark and Steve
    mark = Person("Mark")
    steve = Person("Steve")
    
    print(mark.introduction())
    print(steve.introduction())
    >>> Hi, my name is Mark.
    >>> Hi, my name 
    

    但是请注意,中函数的第一个参数保留给类或对象以将其自身传递给(除非@staticmethod 标签被应用到方法上,然后第一个隐式参数不被传递;它本质上表现为模块方法)。

    还请记住,self 不是保留字,因此您可以将其命名为任何名称(即使self 是 PEP 约定)。下面的示例执行与上面示例相同的输出,并且在语义上是相同的。

    def introduction(myname):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(myname.myname)
    

    9.3.5. Class and Instance Variables

    【讨论】:

      【解决方案3】:

      你的问题是你给你的介绍方法参数myname,但从来没有提供一个有效的参数。你可以简单地做:

      mark = Person(" Mark")
      steve = Person(" Steve")
      
      print(mark.introduction(mark.myname))
      print(steve.introduction(steve.myname))
      

      你给出的介绍方法,你班级里的变量myname

      但上述内容甚至没有必要。由于您在类的 __init__ 方法中初始化了 name 变量,因此它就像一个全局变量。所以你可以简单地说:

      class Person:
          def __init__(self, name):
              """Make a new person with the given name."""
              self.myname = name
      
          def introduction(self):
              """Returns an introduction for this person."""
              return "Hi, my name is{}".format(self.myname)
      
      
      # Use the class to introduce Mark and Steve
      mark = Person(" Mark")
      steve = Person(" Steve")
      
      print(mark.introduction())
      print(steve.introduction())
      

      【讨论】:

        猜你喜欢
        • 2022-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 1970-01-01
        • 2018-11-18
        • 2011-05-23
        相关资源
        最近更新 更多