Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between methods and attributes in python

I am learning python and doing an exercise about classes. It tells me to add nd attribute to my class and a method to my class. I always thought these were the same thing until I read the exercise. What is the difference between the two?

like image 328
Brandon Kheang Avatar asked Sep 20 '17 02:09

Brandon Kheang


People also ask

Is an attribute a method in Python?

There are two kinds of valid attribute names: data attributes and methods. The other kind of instance attribute reference is a method. A method is a function that “belongs to” an object. (In Python, the term method is not unique to class instances: other object types can have methods as well.

What is the difference between methods and data attributes of objects?

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.

What are the different methods in attribute?

Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.

Can methods have attributes in Python?

Everything in Python is an object, and almost everything has attributes and methods.


2 Answers

Terminology

Mental model:

  • A variable stored in an instance or class is called an attribute.
  • A function stored in an instance or class is called a method.

According to Python's glossary:

attribute: A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute a it would be referenced as o.a

method: A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self). See function and nested scope.

Examples

Terminology applied to actual code:

a = 10                          # variable

def f(b):                       # function  
    return b ** 2

class C:

    c = 20                      # class attribute

    def __init__(self, d):      # "dunder" method
        self.d = d              # instance attribute

    def show(self):             # method
        print(self.c, self.d) 

e = C(30)
e.g = 40                        # another instance variable
like image 130
Raymond Hettinger Avatar answered Oct 17 '22 06:10

Raymond Hettinger


A method is an attribute, but not all attributes are methods. For example, if we have the class

class MyClass(object):

    class_name = 'My Class'

    def my_method(self):
        print('Hello World!')

This class has two attributes, class_name and my_method. But only my_method is a method. Methods are functions that belong to your object. There are additional hidden attributes present on all classes, but this is what your exercise is likely talking about.

like image 17
Jeremy McGibbon Avatar answered Oct 17 '22 06:10

Jeremy McGibbon