IOstream
IOstream

Reputation: 113

Is there a lists of built-in methods like (__func__) and how they work in Python 2.7?

I know some of these python built-in methods like: __init__ , __eq__ , __cmp__ . What more are there? Where can I find a good explanation of using these?

Upvotes: 2

Views: 155

Answers (1)

Yam Mesicka
Yam Mesicka

Reputation: 6581

You probably want to take a look at:

Or for Python 3:


The following answers are not as good as the links above. They do not provide precise lists and they are just for research & fun.

But... for the sport, let's build a code that will give you some basic ones :P

names = [funcname for funcname in dir(object) if funcname.startswith('_')]
for name in names:
    print(name)
    print(getattr(object, name).__doc__)
    print('-' * 20)

Or taking it one step further, listing many special functions:

classes = (eval(i) for i in dir(__builtins__)
           if isinstance(eval(i), type) and i != 'type')

magics = {function for one_class in classes for function in dir(one_class)
          if function.startswith('__') and function.endswith('__')}

Or, if you want to be insanly rude, you can even run:*

import re
import requests

TYPEOBJECT_URL = 'https://raw.githubusercontent.com/python' \
                 '/cpython/master/Objects/typeobject.c'
typeobject_c_text = requests.get(TYPEOBJECT_URL).text
print(set(re.findall('__[a-z][a-z0-9_]+__', typeobject_c_text)))

* Probably not all of the functions in the last example are special methods. This is just for the fun.

Upvotes: 3

Related Questions