What is the purpose of __set_name__ in Python descriptors?

Python Professional Hard

Python Professional — Hard

What is the purpose of __set_name__ in Python descriptors?

Key points

  • __set_name__ helps the descriptor identify its own name automatically.
  • It is called when the class is created, not each time the descriptor is set on an instance.
  • It does not set the name of the descriptor class in the owner's namespace.
  • It does not define a custom name for the descriptor in repr().

Ready to go further?

Related questions