• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to know the return object type of functions

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I know a bit of Java, now learning Python. One of the thing I noticed in Java API docs is that the IDE and the docs clearly shows the return type object of a method in the Java class. For e.g. an instance of arrayList.size() returns an int. It is not so intuitive in Python library specs. How do I intuitively figure it out? Is there a simpler way you can suggest?

Thank you,
 
Ranch Hand
Posts: 43
1
Python VI Editor Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thillai,

Unlike Java, Python uses dynamic typing which means that a variable at run time can hold, for example, a string one moment and an integer the next.  Starting with Python 3.5, a more formal way of annotating functions and methods has been defined (or recognized).  The details are given in https://www.python.org/dev/peps/pep-0484/.  As PEP 484 points out, this allows IDE's to identify and flag code that violates the annotation.

The Python annotations are very flexible.  For example, recently I wrote some code that allows a parameter to be a string, integer or an object.  Why?  All three were simply different ways to point to the same object.  This allowed the caller to pass in whatever was handy to identify the desired object.

So, although most library functions returns a specific type which is documented, there are times that a function may be deliberately ambiguous for a reason.  However, no one is suggesting that the documentation is perfect so please share which function is puzzling you.  Perhaps it can be explained better here -- and a request can be submitted to have the documentation improved.  

HTH,
Travis
 
Author
Posts: 8
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shakthi,

Some add-on modules for Python will include the exact argument and return types of their functions in the docs, just like you'd see in Java. The Python Standard Library documentation doesn't do this, but you can usually still figure it out by reading the description of the function and thinking about what makes sense for the function to return. Here's an example:

Python Standard Library documentation wrote:
random.betavariate(alpha, beta)
Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.



The description says the function returns a value "between 0 and 1". So the function has to return a number, but it can't be an int because there are no ints between 0 and 1, so it has to be a float.

If it's hard to figure out the type from the documentation, you can also try opening up a Python console and running the function to see what it returns:



Also, keep in mind that in Python, it doesn't really matter what type a variable is so long as it has the methods and properties you need. (This is sometimes called "duck typing".) If you write x.endswith(".com") in your Python program, x could be a string, but it could also be some other class that has a method named endswith. So another strategy for dealing with types in Python is to not worry too much about them.

- Carter
 
Thillai Sakthi
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Travis Risner wrote:

So, although most library functions returns a specific type which is documented, there are times that a function may be deliberately ambiguous for a reason.  However, no one is suggesting that the documentation is perfect so please share which function is puzzling you.  Perhaps it can be explained better here -- and a request can be submitted to have the documentation improved.  



Thank you. The other day I was working with loading a JSON file and trying to parse through and finding elements in it. (https://docs.python.org/3/library/json.html#basic-usage). json.load documentation simply said "it returns a python object". I was not sure what object was returned. I simply coded something and the exception told me it was a dict object, then I was able to navigate through it. Now I see the document specifically this link (https://docs.python.org/3/library/json.html#json-to-py-table), it makes more sense. Once I crossed this first issue I had, in fact, parsing through the JSON file was so intuitive and able to get the job done in very little time...
 
Thillai Sakthi
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carter Sande wrote:

Also, keep in mind that in Python, it doesn't really matter what type a variable is so long as it has the methods and properties you need. (This is sometimes called "duck typing".) If you write x.endswith(".com") in your Python program, x could be a string, but it could also be some other class that has a method named endswith. So another strategy for dealing with types in Python is to not worry too much about them.



Thank you. This is the first time I am working with a dynamic typed language. So, please pardon my ignorance. Because it is dynamically typed, python could be slow in performance compared to static type languages if I were to write a high throughput API for instance? Any tips/suggestions to be aware of?
 
Carter Sande
Author
Posts: 8
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shakthi,

Python often does perform slower than languages like Java. Part of that is because Python is dynamically typed like you mention, but part of it is just that the Python interpreter doesn't have fancy features like the "Just-In-Time compilation" that Java uses to run more quickly. (If you're interested, there's a project called PyPy that's trying to make a faster Python interpreter using JIT.)

If you're just starting out, the best advice I can give you is to try and keep your programs simple. The less work the computer has to do to run your program (calling less functions, making less objects, and so on), the faster it'll run.

Another way to help make your Python programs faster is to use functions written in other languages. C code, for example, runs more quickly but is a lot harder to write than Python code. So people have created modules like Pygame and NumPy that are mostly written in C, with an interface to use that fast C code from your Python program. A lot of Python's built-in functions are also written in C for that reason.

- Carter
 
A teeny tiny vulgar attempt to get you to buy our stuff
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!