In [1]:
import numpy as np

In [2]:
l = [0, 1, 2]
print(type(l))


<class 'list'>

In [3]:
print(hasattr(l, 'append'))


True

In [4]:
print(hasattr(l, 'xxx'))


False

In [5]:
print(len(l))


3

In [6]:
print(l.__len__())


3

In [7]:
def print_len_hasattr(x):
    if hasattr(x, '__len__'):
        print(len(x))
    else:
        print('x has no __len__')

In [8]:
print_len_hasattr([0, 1, 2])


3

In [9]:
print_len_hasattr('abc')


3

In [10]:
print_len_hasattr(100)


x has no __len__

In [11]:
a = np.arange(3)
print(a)


[0 1 2]

In [12]:
print_len_hasattr(a)


3