Python __class__()

2024/9/8 9:21:37

In Python's documentation __class__ is described as an attribute. In the object type (the metaclass), __class__ appears to be a method.

If we do:

>>> class Foo:pass>>> a = Foo()
>>> a.__class__ == type.__class__(a)
True

So, my questions are:

  1. When we call a.__class__, are we really calling the method type.__class__(a)?
  2. Is this the reason why __class__ is not a member of the __dict__ attribute of a?
Answer

__class__ is a data descriptor object. Many attributes on Python core objects are implemented as descriptors. You should see that as an implementation detail, nothing more.

__class__ is a descriptor because Python needs to be able to validate new values you assign to it; there are certain limitations to assigning to __class__ that need to be honoured, and making __class__ a descriptor is the most efficient method of doing so.

Descriptor objects are automatically found and invoked on the type when you try to access the attribute on an object. instance.__class__ will find and execute the __class__ descriptor on the class (by searching through all classes in the inheritance graph), typically ending up with object.__dict__['__class__'].__get__(instance, type(instance)) (where object is usually the first class on which the __class__ attribute is found in the type(instance).__mro__ sequence); This happens because Python will always use the type's __getattribute__ method to find attributes, and that method knows how to handle descriptors found on the class and bases, as well as look at the object.__dict__ attributes. So they don't live on the object __dict__ itself, they live on the object type, by design.

Now, class objects are also callable objects. That's how you create an instance; for a given class Foo, you create an instance by calling it, so Foo(). instance.__class__ is just a reference to the class object, just like class_obj = Foo would create a reference to a class. Calling the class object produces a new instance, whatever reference you used to get to it.

Finally, type.__class__ is just a reference to type() itself:

>>> type.__class__ is type
True
>>> type.__class__
<class 'type'>
>>> type(type)
<class 'type'>

That's because type is its own type. The parent-child relationships of the Python type system must stop somewhere, and type is that point.

In your example, a.__class__ is a reference to the Foo class. And type.__class__ is the same object as type, so you essentially did this:

Foo == type(a)

which is indeed true, the type of a is Foo.

https://en.xdnf.cn/q/73211.html

Related Q&A

how to handle ever-changing password in sqlalchemy+psycopg2?

I inherited some code that uses sqlalchemy with psycopg2, which needs to run on AWS. RDS Postgres supports iam-based authentication, but the way it does it is rather kludgy: you request a temporary pas…

How to determine which compiler was requested

My project uses SCons to manage the build process. I want to support multiple compilers, so I decided to use AddOption so the user can specify which compiler to use on the command line (with the defaul…

Does pytest have anything like google tests non-fatal EXPECT_* behavior?

Im more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_* vs EXPECT_* which are the fatal and non-fatal assert modes.From the documentation:Th…

Radon transformation in python

Here is a dummy code:def radon(img):theta = np.linspace(-90., 90., 180, endpoint=False)sinogram = skimage.transform.radon(img, theta=theta, circle=True)return sinogram # end defI need to get the sinogr…

Librosa raised OSError(sndfile library not found) in Docker

Im trying to write the Dockerfile for a small python web project and there is something wrong with the dependencies. Ive been doing some search on the internet and it said that Librosa library requires…

Implementing Tags using Django rest framework

TDLR : what is the best way to implement tags in django-rest-framework. where the tags has a created_by field which is the currently authenticated user.I am trying to achieve a very simple/common thing…

Python audiolab install, unable to install (or find) libsndfile on Mac OSX

Trying to install scikits.audiolab-0.11.0 on Mac, bit it requires libsndfile: http://www.mega-nerd.com/libsndfile/. I did install libsndfile supposedly, using libsndfile_python-1.0.0-py2.7-macosx10.5.m…

Connecting to events of another widget

This is most likely a duplicate question, but I have to ask it because other answers arent helping in my case, since I am new to pyqt (switched from tkinter few days ago).I am wondering if is it possib…

Diff multidimensional dictionaries in python

I have two dictionariesa = {home: {name: Team1, score: 0}, away: {name: Team2, score: 0}} b = {home: {name: Team1, score: 2}, away: {name: Team2, score: 0}}The keys never change but I want to get that …

Pandas DatetimeIndex vs to_datetime discrepancies

Im trying to convert a Pandas Series of epoch timestamps to human-readable times. There are at least two obvious ways to do this: pd.DatetimeIndex and pd.to_datetime(). They seem to work in quite dif…