What are "objects" like Python?

I'm just starting to learn Python. I find the type system a little difficult to understand. I have a number of questions, but first of all a short reduction of a short story; The documentation states:

"All data in a Python program is represented by objects ... Each object has an identifier, type, and value."

No problems. But in addition, it is not actually described what "objects" are. For example, documents do not even cover the fact that these "objects" support the point operator - from my PoV they can be part of a memory data structure that is not exposed to the user outside id(), type()etc. However, I understand that there is some basic metaobject interface similar to that described for an instance type object in documents. To work with an example:

If I do this on an instance of class "x":

x.__class__.__name__

I get the name of my class. I understand it. The documentation describes the properties of __class__both __name__class instances and class type objects. If I do this [].__class__.__name__, I will get a "list". Similarly int(1).__class__.__name__it gives "int". Its ambiguity for me is exactly what is happening under the hood, and I would like to clarify. So my questions are:

  • What is the relationship between object types of type "objects" and "objects of class instances"?
  • Can we assume that the ~ meta API for built-in objects of the type is the same as for objects of the type of an instance of a class?
  • If so, what is this interface and where is it documented?
  • In general, what are “objects” that correspond to built-in types and how are they implemented?
+4
source share
3

1,2, 4- 3-:

  • " " "" "??
  • " , -API , ? "

, , API. "", "", - ""... .

  • " , ""..."

Python, , OOPL. Python , OOPL, . object Python. , object "" object - .

, Python (2,2 ) "" "" ( ). , "int", - (, , ). , x = int(1) int () int, x.

, Python ; , , "-" - , . , ; , . : Python - , . , "". type . , , __bases__ . , , object. . http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html.

  • "... ?"

, . > , . , ( ), . .

+6

Python - , ( - ). , , , .

- , , .

Python 3 . "" ( ), "" (, , , ) .

( , -, , Python, . , , ).

() - , , .

__class__ ( type() ). , , , , . .

+1

, .

- . Python, , , .. , type. , , , , .

>>> type(int)
<type 'type'>
>>> type(1)
<type 'int'>

>>> class Foo(object):
...   pass
>>> type(Foo)
<type 'type'>
>>> obj = Foo()
>>> type(obj)
<class '__main__.Foo'>

( type(x) x.__class__.)

0

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


All Articles