0

For example I have a poorly documented library. I have an object from it and I want to know what are the types of the arguments certain method accepts.

In IPython I can run

In [28]: tdb.getData?
Signature: tdb.getData(time, point_coords, sinterp=0, tinterp=0, data_set='isotropic1024coarse', getFunction='getVelocity', make_modulo=False)
Docstring: <no docstring>
File:      ~/.local/lib/python3.5/site-packages/pyJHTDB/libJHTDB.py
Type:      method

but it does not give me the types of the arguments. I don't know exactly what is the type of point_coords.

6
  • 4
    If it's a native python function, there are no strict types of names in the C sense. You could even pass a ham sandwich as point_coords. Commented Sep 29, 2016 at 17:15
  • Unless it uses the new type annotations feature, that is. Commented Sep 29, 2016 at 17:20
  • Read the source and look for relevant comments and what operations they try to perform on the argument, or look for usage examples. The Github page says to look at the test_plain function, which seems to indicate point_coords should be an Nx3 NumPy array of float32 dtype. Commented Sep 29, 2016 at 17:20
  • 1
    you can try to use two question marks instead of one in interactive (??) to see the function definition. It might help to understand what is expected. Commented Sep 29, 2016 at 17:20
  • Python is dynamically typed (if it quacks like a duck...) so any object that "looks" like the expected type works for a calculation but that makes your task difficult. You need to poke around the code (perhaps find other consumers) to see what the requirements are. Commented Sep 29, 2016 at 17:23

1 Answer 1

2

Usually, functions in Python accept arguments of any type, so you cannot define what type it expects.

Still, the function probably does make some implicit assumptions about the received object.

Take this function for example:

def is_long(x):
    return len(x) > 1000

What type of argument x does this function accept? Any type, as long as it has length defined.

So, it can take a string, or a list, or a dict, or any custom object you create, as long as it implements __len__. But it won't take an integer.

is_long('abcd')  # ok
is_long([1, 2, 3, 4])  # ok
is_long(11)  # not ok

To answer the question: How can you tell what assumtions the function makes?

  • read the documentation
  • read the doc string (try help(funcname))
  • guess: Pass any argument to it and see how it fails. If it fails with AttributeError: X instance has no attribute 'get_value', it expects something with get_value.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.