-1

From https://docs.python.org/3/reference/expressions.html#calls

The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a __call__() method are callable).

  1. Does "class objects" refer to "instances of classes" or "classes themselves as objects"? I guess it tries to mean the latter, but the wording seems to mean the former. If I am correct, in C++ and Java, objects always mean instances of some classes, and that is probably the cause of my confusion. What are some names that are used to refer to "instances of classes"?
  2. Does "methods of class instances" mean "functions as attributes of instances of some classes" or "functions as attributes of some classes which operate on instances of the classes"? I guess it tries to mean the latter, but the wording seems to mean the former. An instance of a class may have an attribution which isn't an attribute of the class and which is a function:

    class C:
        pass
    
    c=C()
    def f():
        pass
    
    c.myfun=f
    

    where c.myfun is a function attribute of instance c, not a function attribute of class C.

    What are some names commonly used to refer to the attribute of an instance of a class, which is not an attribute of a class?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

2

Class objects refer to class objects.. not instances of that class.

>>> C
<class __main__.C at 0x01FA89D0>

Everything in python is an object. There are first class objects (here's a great answer about this subject https://stackoverflow.com/a/245208/840582).

'Methods of class instances' refers to methods (functions) defined inside the class and bounded to an instance

class C:
    pass

    def inst_f(self):
        pass

>>> c
<__main__.C instance at 0x0293A5F8>
>>> c.inst_f
<bound method C.inst_f of <__main__.C instance at 0x0293A5F8>>
>>> f
<function f at 0x02986070>

Class instances are objects created using that class; a = C() a is an instance of C. You can see that in <__main__.C instance at 0x0293A5F8>

In general, everything you see inside a class definition code that starts with self is an attribute of instance of the class. These are referred as instance variables.

Class variables (that is, all objects actually share the same variable) are defined without self. prefix. So for example

class C:
    cls_var = 'static'  # this is a class attribute

    def __init__(self, x):
        self.x = x  # this is an instance attribute 

    def inst_f(self):
        pass

a = C('this is a')
b = C('this is b')

print a.x is b.x  # False

print a.cls_var  # static

print a.cls_var is b.cls_var  # True
Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • Thanks. What are some names that are used to refer to "instances of classes"? What are some names commonly used to refer to the attribute of an instance of a class, which is not an attribute of a class? – Tim Sep 17 '17 at 14:40