Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Class Attributes, Instance Attributes, and Instance Methods in Python

Tags:

python

I know similar questions to this have been asked before, but every website seems to define these things differently, and I am trying to follow along the official documentation: From what I understand so far:
- Class attributes are everything listed in the class Classname: code block, including the variable declarations above the __init__ functions, static methods, class methods, and methods involving instances with self
link for more info: https://docs.python.org/2/tutorial/classes.html#class-objects
- Instance attributes are both: 1.) every function listed in the class Classname: code block so everything other than the variable declarations above the __init__ function & 2.) data attributes which is when we use instance.attr and set that equal to some value
link for more info: https://docs.python.org/2/tutorial/classes.html#instance-objects

Assuming everything I wrote above is correct, I would say an Instance method is any function in the class Classname: code block since we can apply each method to the instance objects or it could just be the functions that involve self

I'm not sure what I'm understanding incorrectly, but the terminology of the python documentation is totally different from the other sites I've seen. Any help would be greatly appreciated. Thanks!

like image 316
Slade Avatar asked Mar 14 '23 05:03

Slade


2 Answers

Class attributes are same for all instances of class whereas instance attributes is particular for each instance. Instance attributes are for data specific for each instance and class attributes supposed to be used by all instances of the class.

"Instance methods" is a specific class attributes which accept instance of class as first attribute and suppose to manipulate with that instance.

"Class methods" is a methods defined within class which accept class as first attribute not instance(that's why the are class methods).

You can easily see class attributes by accessing A.__dict__:

class A:
   class_attribute = 10
   def class_method(self):
       self.instance_attribute = 'I am instance attribute'

print A.__dict__
#{'__module__': '__main__', 'class_method': <function class_method at 0x10978ab18>, 'class_attribute': 10, '__doc__': None}

And instance attributes as A().__dict__:

a = A()
a.class_method()
print a.__dict__
# {'instance_attribute': 'I am instance attribute'}

Some useful links from official python documentation and SO, which i hope won't confuse you more and give clearer understanding...

  • Python Classes
  • Dive into python, 5.8. Introducing Class Attributes
  • Definition of python getattr and setattr
  • What are Class methods in Python for?
  • Difference between Class and Instance methods
like image 72
Andriy Ivaneyko Avatar answered Apr 30 '23 01:04

Andriy Ivaneyko


To answer this question, you need to first think about how python looks up attributes. I'll assume you know what an instance is. As a refresher. . .

class SomeClass(object):

    class_attribute = 'This -- Defined at the class level.'

    def __init__(self):
        self.instance_attribute = 'This -- Defined on an instance.'

    def method(self):
        pass

instance = SomeClass()

Class attributes are defined at the class level. Instance attributes are defined at the instance level (usually via self.xyz = ...). Instance attributes can also be monkey patched onto the instance:

instance.another_instance_attribute = '...'

Notice that class attributes can be looked up on an instance if an instance attribute doesn't "shadow" it.

print(instance.class_attribute)

Now for methods. Methods are another animal entirely.

SomeClass.method

is definitely an attribute on the class.

instance.method

is a bit trickier to classify. Actually, when python sees that statement, it actually executes the following:

SomeClass.method.__get__(instance, SomeClass)

Weird. So what is happening here is that functions are descriptors. Since they have a __get__ method, it gets called and the return value (in this case) is a new function that knows what self is. The normal terms are "instance method" and "bound method". I suppose some might consider it an instance attribute, but, I'm not sure that's exactly correct (even though you access it via the instance)

like image 24
mgilson Avatar answered Apr 30 '23 01:04

mgilson