4

I've realized that some methods should be called with (), while others can't. How can I check, using IPython e.g., whether to use parentheses or not? For example the following file scratch.py

import numpy as np

arr = np.random.randn(5)

print arr.sort, "\n"
print arr.sort(), "\n";
print arr.shape, "\n";
print arr.shape(), "\n";

produces this output:

<built-in method sort of numpy.ndarray object at 0x7fb4b5312300> 

None 

(5,) 

Traceback (most recent call last):
  File "scratch.py", line 8, in <module>
    print arr.shape(), "\n";
TypeError: 'tuple' object is not callable
Ren
  • 2,852
  • 2
  • 23
  • 45
bcf
  • 2,104
  • 1
  • 24
  • 43

2 Answers2

6

Those are not methods, those are properties. The descriptor is invoked behind the scenes by Python itself.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Methods in Python are always invoked with a (). Best way to check if something is a method is to read the documentation of the library.

John Chang
  • 131
  • 2