使用' self'仅作为方法中的参数?

时间:2017-07-12 21:49:01

标签: python class

我之前创建了这个类。

class Dog():
    """A simple attempt to model a dog."""

    def __init__(self, name, age):
        """Initialise name and age attributes."""
        self.name = name
        self.age = age

    def sit(self):
        """Simulate a dog sitting in response to a command."""
        print(self.name.title() + " is now sitting.")

现在,我正在阅读的这本书说明了以下内容

  

因为这些方法不需要额外的信息,如姓名或年龄,我们只需将它们定义为一个参数self。

我对这意味着什么感到困惑。

这是否意味着当我们传递参数self时,该方法可以自动访问属性self.nameself.age

这本书是Eric Mattes的 Python Crash Course

0 个答案:

没有答案