Difference between class attributes, instance attributes, and instance methods in Python

I know that similar questions were asked earlier, but each website seems to define these things differently, and I try to follow the official documentation: From what I understand so far:
- The class attributes are all listed in the code block class Classname:, including variable declarations of the above functions __init__, static methods, class methods, and instance methods using the self
link for more information: https://docs.python.org/2/tutorial/classes.html#class-objects
- Instance attributes: 1.) each function specified in the code block class Classname:, therefore, everything, except e variable declarations above functions __init__and 2.) that are used when we use instance.attrand set equal to some value
link for more information: https://docs.python.org/2/tutorial/classes.html#instance-objects

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

I'm not sure what I misunderstand, but the python documentation terminology is completely different from the other sites I've seen. Any help would be greatly appreciated. Thank!

+4
source share
4 answers

, . , , .

" " - , .

" " - , , ( ).

, 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}

A().__dict__:

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

python SO, , , ...

+3

, , python . , , . .,.

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()

. ( self.xyz = ...). :

instance.another_instance_attribute = '...'

, , "".

print(instance.class_attribute)

. - .

SomeClass.method

, .

instance.method

. , python , :

SomeClass.method.__get__(instance, SomeClass)

Weird. , . __get__, , ( ) , , self. " " " ". , , , ( )

+3

-   Classname: code block, .

, instance.attr self.attr( self )

class Base:
  cls_var = "class attribute"
  def __init__(self):
    self.var = "instance attribute"
  def method(self):
    self.var1 = "another instance attribute"
  cls_var1 = "another class attribute"

. . , . , , .

class Base:
  cls_var = "class attribute"
  def __init__(self):
    self.var = "instance attribute"
b = Base()
print b.var                                     # it should print "instance attribute"
print b.cls_var                                 # it should print "class attribute"
print Base.cls_var                              # it should print "class attribute"
b.cls_var = "instance attribute"
print Base.cls_var                            # it should print "class attribute"
print b.cls_var                                # it should print "instance attribute"

. , def , , object (self). , , .

>>> class Base:
    def ins_method(self):
        pass


>>> b = Base()
>>> Base.__dict__
{'__module__': '__main__', '__doc__': None, 'ins_method': <function ins_method at 0x01CEE930>}
>>> b.__dict__
{}

, -

Base.ins_method(Base())

, , - http://www.pythonabc.com/python-class-easy-way-understand-python-class/ http://www.pythonabc.com/instance-method-static-method-and-class-method/

0

, , . , , , python. , .

- , . , clock, .

class Clock:
    class_hours = 12

.

- , , . , clock Class, , ( , , ):

class Clock:
    class_hours = 12

    def __init__(self, hours):
        if (hours <= class_hours):
            self.hours = hours

, if , , , . , , , .

- , , , . clock, , , :

class Clock:
    class_hours = 12
    class_minutes = 60
    class_seconds = 60

    def __init__(self, hours, minutes, seconds):
        self.hours = hours
        self.minutes = minutes
        self.seconds = seconds

    def setTime(new_hour, new_minute, new_second):
        self.hours = new_hour
        self.minutes = new_minute
        self.seconds = new_second

clock, - . , clock.setTime(11,49,36) 11:49:36.

I hope this was helpful and as clear as possible to you. If I were ambiguous or ignored any terminology that you do not understand, let me know so that I can update my answer in order to be as clear as possible.

0
source

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


All Articles