Differences between data attributes and method attributes

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

I read python 2.7.9 ( https://docs.python.org/2/tutorial/classes.html#random-remarks ) and suddenly both became hard to understand. I will take care of it.

+16
source share
5 answers

An attribute is a variable that is viewed on another object using the dotted syntax: obj.attribute . The Python path is designed, attribute searching can do many things, and this variety can sometimes lead to errors if you really don’t understand what is happening (this is what documentation related information warns about).

The most basic problem is that the attribute search can either find the value stored in the dictionary of the instance of the object, or it can find something from the class of the object (or the base class if inheritance occurs). Methods are functions stored in a class, but you usually use them by looking at them on an instance (which “binds” the method, inserting the object as the first argument when the method is called).

The exact sequence of what is checked when a bit complicated (I described the complete process in an answer to another question ), but at the most basic level, instance attributes usually take precedence over a class attribute.

If an instance attribute and a class attribute with the same name exist, usually only the instance attribute is available. This can be very confusing if it is unintentional.

Consider the following code:

 class Foo(object): def __init__(self, lst): self.lst = lst def sum(self): self.sum = sum(self.lst) return self.sum f = Foo([1,2,3]) print(f.sum()) print(f.sum()) 

At the bottom of this code, we make two identical calls. The first works just fine, but the second throws an exception.

This is due to the fact that the first f.sum we search for f.sum we find a method in the Foo class. We can name the method without problems. The problem is that the sum method assigns the result of its calculation (the sum of the elements in self.lst ) to the instance attribute, also called sum . This hides the sum method from the view.

When the second call to f.sum() looks up f.sum , it finds an instance attribute containing the integer 6 , not the expected method. An integer is not callable, so we get an exception.

The solution, of course, should not use the same name for the method and attribute. The above code is a pretty simple example. Errors caused by such things in more complex code can be much harder to figure out.

If you write code that adds attributes to objects that you know little about, you should be careful to avoid common names. If you are writing a mixin class, consider using two leading underscores in attribute names to trigger the Python name manipulation, which is designed specifically for this kind of situation.

+22
source

An attribute is any thing due to the lack of a better word associated with 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 a name that is just a value bound to the Dog instance. As for the attribute of the method, one answer would be the cortex method, since it is not so much a value as an action. This is the same as in English. The data attribute is exactly as it appears; it's data, it's just a property. A method is a procedure, an action, and it is this attribute of a method.

+9
source

Here is a direct explanation of your question that helped me understand the difference between an attribute and a method.

A class is like a set of instructions or a plan on how to build many objects that share characteristics.

An object is a data type built in accordance with the specifications provided by the class definition.

An attribute is a value (characteristic). Think of an attribute as a variable that is stored inside an object.

A method is a set of instructions. Methods are functions associated with an object. Any function included in the definition of a parent class can be called by an object of this class.

Hope this helps.

+3
source

An attribute is all you can do instance.attribute_name with. For example, in:

 class Hello(object): def __init__(self, word): self.word = word def greet(self): print "Hello: "+self.word 

__init__ , greet and word will be all attributes. I would suggest that a method is everything that is declared with def in the class scope (as opposed to doing self.func = lambda x: x * x, for example). In this case, you get into related or unrelated methods and the like. The important part is that for the member attribute, when you do instance.method_name , you return a bound method that, when you call it, will call the original method with the instance as the first argument.

In addition, after reading some of these sections, their wording is somewhat confused / erroneous. For example, they say: “Attributes of attribute attributes redefine attributes of a method with the same name”, which, as far as I know, is better placed as an attribute of an instance, override class attributes with the same name. From the example that I gave if we expanded it to:

 class Hello(object): greeting = "Hello: " def __init__(self, word): self.word = word def greet(self): print self.greeting+self.word 

Then we could do:

 >>> a = Hello("world") >>> a.greeting = "Goodbye " >>> a.greet() "Goodbye world" 

This is because we place the welcome instance attribute on top of the welcome class attribute. Since the methods defined in the class (the usual way) are class attributes, they will be overridden by any instance attributes (data or otherwise).

+2
source

An attribute describes an object, while a method acts on an object and modifies it.

0
source

Source: https://habr.com/ru/post/983195/


All Articles