Differences between data attributes and method attributes

前端 未结 5 804
南旧
南旧 2020-12-23 15:26

What is a method attribute, and a data attribute? What the difference between them and what they have in common?

I was reading python 2.7.9 (https://docs.python.org/

5条回答
  •  一整个雨季
    2020-12-23 15:32

    An attribute is any thing for the lack of a better word that is bound to an object, for example:

    class Dog:
        def __init__(self):
            self.name = "Rufus"
    
        def bark(self):
            print "Woof Woof!"
    

    In this case the data attribute is the name, which is simply a value that is bound to the instance of the Dog. As for a method attribute, one answer would be the bark method, as it's not so much a value as it is an action. It's just as it is in English. A data attribute is exactly as it sounds; it's data, it is simply a property. A method is a procedure, an action, and this is exactly what a method attribute is.

提交回复
热议问题