views:

121

answers:

3

Groovy has a nice operator for safe dereferencing, which helps to avoid NullPointerExceptions:

variable?.method()

The method will only be called, if variable is not null.

Is there a way to do the same in Python? Or do I have to write if variable: variable.method()?

+2  A: 

Python has no such thing. This is because there is no null pointer in Python. Well, there is the special None value, which is often used in situations, where it represents "no value". But that is just a convention. None is a value/object like all others. And since there is no null, there is no operator to deal with it.

Dirk
I agree, but `None` is essentially the same in this case. Instead of a `NullPointerException` I would get an `AttributeError`, but I could not call a method on `None` which would be present in usual object such as a list.
deamon
The point is, that `None` is not "unusual" at all. It is an object with a proper class, has methods, etc. There is nothing in the language which would make it special in any way (besides, that it is being used to represent the "nothing interesting here" value). So, what would the hypothetical `?.` operator test for? For `None` specifically? What about other libraries which use other markers (e.g., `NOT_FOUND` in PEAK)?
Dirk
@Dirk: `None` is great for marking optional function arguments and various other things, but using it (or something like `NOT_FOUND`) as a "not found" result or other special return value should be avoided. Using exceptions for these kinds of exceptional conditions is better because it tends to simplify code using those functions (instead of checking every return value, you can have an except clause, or just let the exception bubble up).
adw
+2  A: 
  1. No, there isn't.

  2. But to check for None, you don't write if x:, you write if x is None:. This is an important distinction - x evaluates to False for quite a few values that are propably perfectly valid (most notably 0-equivalent numbers and empty collections), whereas x is None only evaluates to True if the reference x points to the singleton object None.

  3. From personal experience, such an operator would be needed very rarely. Yes, None is sometimes used to indicate no value. But somehow - maybe because idiomatic code returns null objects where sensible or throws exceptions to indicate critical failure - I only get an AttributeError: 'NoneType' object has no attribute '...' twice a month.

  4. I would argue that this might be a misfeature. null has two meanings - "forgot to initialize" and "no data". The first is an error and should throw an exception. The second case usually requires more elaborate handling than "let's just not call this method". When I ask the database/ORM for a UserProfile, it's not there and I get null instead... do I want to silently skip the rest of the method? Or do I really want to (when in "library code") throw an approriate exception (so "the user (code)" knows the user isn't there and can react... or ignore it) or (when I'm coding a specific feature) show a sensible message ("That user doesn't exist, you can't add it to your friend list") to the user?

delnan
+2  A: 

An idiom I have seen and used is callable(func) and func(a, b, c) in place of a plain function call (where the return value is not used). If you are trying to use the return value, however, this idiom will yield False if the function is not callable, which may not be what you want. In this case you can use the ternary operator to supply a default value. For example, if the function would return a list that you would iterate over, you could use an empty list as a default value with func(a, b, c) if callable(func) else [].

kindall