Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are __properties__ called in Python?

Tags:

python

I'm trying to figure out the proper name for these properties which are written using underscores, so that I can read about them and understand them more. They seem to generally be lower level things, more advanced stuff for really explicit behavior.

What terminology is used for these underscore properties/methods?

like image 735
temporary_user_name Avatar asked Jun 29 '26 19:06

temporary_user_name


2 Answers

"Magic Methods". You can learn more about them here: http://docs.python.org/2/reference/datamodel.html#basic-customization

Important ones are:

  • __init__(): Constructor for a class
  • __str__() (or __unicode__(): verbose name of the object used whenever string conversion is needed (e.g. when calling print my_object

I'd say those are the one you'll need in the beginning.

like image 175
OBu Avatar answered Jul 01 '26 07:07

OBu


"Magic methods" is a term often used for those that are methods. "Double-underscore" is also sometimes used.

PEP 8 describes them as "magic".

like image 40
BrenBarn Avatar answered Jul 01 '26 08:07

BrenBarn