Python dereferencing safely

Groovy has a good statement for safe dereferencing, which helps to avoid NullPointerExceptions:

variable?.method() 

method will be called if variable not null .

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

+7
python nullpointerexception null groovy
source share
4 answers
  • No no.

  • But to check for None you do not write if x: you write if x is None: This is an important difference - x is evaluated to False for a range of values ​​that are maximally valid (primarily 0-equivalent numbers and empty collections), while x is None only evaluates True if the reference x points to a single None object.

  • From personal experience, such an operator is very rarely needed. Yes, None sometimes used to indicate meaning. But for some reason - maybe because the idiomatic code returns null objects where reasonable or throws exceptions that indicate a critical failure - I get only AttributeError: 'NoneType' object has no attribute '...' twice a month.

  • I would say that this could be a failure. 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 complex processing than "let it just not call this method." When I query the / ORM database for UserProfile , it is not there and I get null instead ... do I want to skip the rest of the method? Or I really want (when in the "library code") to throw a corresponding exception (so that "user (code)" knows that the user does not exist and can respond ... or ignore it) or (when I am encoding a certain function) to show a reasonable message ("This user does not exist, you cannot add it to your friends list") to the user?

+10
source share

Python has no such thing. This is because in Python there is no null pointer. Well, there is a special None value that is often used in situations where it represents "no value". But this is just a convention. None - value / object, like everyone else. And since there is no null , the operator has no operator.

+3
source share

The idiom I saw and use is callable(func) and func(a, b, c) instead of just calling a function (where the return value is not used). However, if you are trying to use the return value, this idiom will give False if the function cannot be called, which may not be what you want. In this case, you can use the ternary operator to supply the default value. For example, if a function returns a list that you would list, you can use the empty list as the default value with func(a, b, c) if callable(func) else [] .

+3
source share

I used this feature in Groovy, so I will not repeat the blub paradox of other posters.

In Groovy, such an operator

 if(possiblyNull?.value){ ... 

This is in Python

 try: testVar = possiblyNull.value except: testVar = None if(testVar): 

This is definitely a great feature in Groovy, and it helps in removing syntax noise. There are several other bits of syntactic sugar, such as the Elvis operator or *, but they sacrifice readability as an expense for quickly correcting characters (in other words, they are not pythons).

Hope that helps :-)

+2
source share

All Articles