Skip to content

Overload Assignment Operator Python Eats

String interpolation

Any Groovy expression can be interpolated in all string literals, apart from single and triple single quoted strings. Interpolation is the act of replacing a placeholder in the string with its value upon evaluation of the string. The placeholder expressions are surrounded by or prefixed with for dotted expressions. The expression value inside the placeholder is evaluated to its string representation when the GString is passed to a method taking a String as argument by calling on that expression.

Here, we have a string with a placeholder referencing a local variable:

But any Groovy expression is valid, as we can see in this example with an arithmetic expression:

Not only expressions are actually allowed in between the placeholder. Statements are also allowed, but a statement’s value is just . So if several statements are inserted in that placeholder, the last one should somehow return a meaningful value to be inserted. For instance, "The sum of 1 and 2 is equal to ${def a = 1; def b = 2; a + b}" is supported and works as expected but a good practice is usually to stick to simple expressions inside GString placeholders.

In addition to placeholders, we can also use a lone sign prefixing a dotted expression:

But only dotted expressions of the form , , etc, are valid, but expressions that would contain parentheses like method calls, curly braces for closures, or arithmetic operators would be invalid. Given the following variable definition of a number:

The following statement will throw a because Groovy believes you’re trying to access the property of that number, which doesn’t exist:

You can think of as being interpreted by the parser as .

If you need to escape the or placeholders in a GString so they appear as is without interpolation, you just need to use a backslash character to escape the dollar sign:

Special case of interpolating closure expressions

So far, we’ve seen we could interpolate arbitrary expressions inside the placeholder, but there is a special case and notation for closure expressions. When the placeholder contains an arrow, , the expression is actually a closure expression — you can think of it as a closure with a dollar prepended in front of it:

1The closure is a parameterless closure which doesn’t take arguments.
2Here, the closure takes a single argument, to which you can append content with the leftShift operator. In either case, both placeholders are embedded closures.

In appearance, it looks like a more verbose way of defining expressions to be interpolated, but closures have an interesting advantage over mere expressions: lazy evaluation.

Let’s consider the following sample:

1We define a variable containing that we then interpolate within two GStrings, as an expression in and as a closure in .
2We expect the resulting string to contain the same string value of 1 for .
3Similarly for
4

What is the null or None Keyword

The keyword is commonly used in many programming languages, such as Java, C++, C# and Javascript. It is a value that is assigned to a variable. Perhaps you have seen something like this:

null in Javascript

null in PHP

null in Java

The concept of a keyword is that it gives a variable a neutral, or "null" behaviour. Note that technically the behaviour of changes between higher and lower-level languages, so to keeps things simple we'll be referring to the concept in object-orientated languages.

Python's null Equivalent: None

The equivalent of the keyword in Python is . It was designed this way for two reasons:

  • Many would argue that the word "null" is somewhat esoteric. It's not exactly the most friendliest word to programming novices. Also, "None" refers exactly to the intended functionality - it is nothing, and has no behaviour.
  • In most object-oriented languages, the naming of objects tend to use camel-case syntax. eg. . As you'll see soon, Python's type is an object, and behaves as one.

The syntax to assign the type to a variable, is very simple. As follows:

Why Use Python's None Type?

There are many cases on why you would use .

Often you will want to perform an action that may or may not work. Using is one way you can check the state of the action later. Here's an example:

Another scenario would be where you may need to instantiate a class, depending on a condition. You could assign a variable to , then optionally later assign it to an object instance. Then after that you may need to check if the class has been instantiated. There are countless examples - feel free to provide some in the comments!

Python's None is Object-Orientated

Python is very object-orientated, and you'll soon see why. Note that the keyword is an object, and behaves as one. If we check what type the object is, we get the following:




>>>type(None)

<class'NoneType'>




>>>type(None)

<type'NoneType'>


We can discover three things from this:

  • is an object - a class. Not a basic type, such as digits, or and .
  • In Python 3.x, the object was changed to new style classes. However the behaviour of is the same.
  • Because is an object, we cannot use it to check if a variable exists. It is a value/object, not an operator used to check a condition.

Checking if a Variable is None

There are two ways to check if a variable is . One way can be performed by using the keyword. Another is using the syntax. Both comparison methods are different, and you'll see why later:

This code will give us the following output:

Great, so they're the same! Well, sort of. With basic types they are. However with classes you need to be careful. Python provides classes/objects with the ability to override comparison operators. So you can compare classes, for example . This article won't go into depth on how to override comparison operators in classes, but it should provide insight as to why you should avoid checking if a variable is using the syntax.

That gives us the following output:

Interesting! So you can see that the keyword checks if two objects are exactly the same. Whereas the operator first checks if the class has overridden the operator. For the PHP coders out there, using the syntax is the same as in Python, where using the keyword is equivalent to the syntax.

Because of this, it's always advisable to use the keyword to check if two variables are exactly the same.

1

varnull_variable=null;

1

SomeObject null_object=null;

1

my_none_variable=None

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

# We'd like to connect to a database. We don't know if the authentication

# details are correct, so we'll try. If the database connection fails,

# it will throw an exception. Note that MyDatabase and DatabaseException

# are not real classes, we're just using them as examples.

 

database_connection=None

 

# Try to connect

try:

    database=MyDatabase(db_host,db_user,db_password,db_database)

    database_connection=database.connect()

exceptDatabaseException:

    pass

 

ifdatabase_connection isNone:

    print('The database could not connect')

else:

    print('The database could connect')

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

null_variable=None

not_null_variable='Hello There!'

 

# The is keyword

ifnull_variable isNone:

    print('null_variable is None')

else:

    print('null_variable is not None')

 

ifnot_null_variable isNone:

    print('not_null_variable is None')

else:

    print('not_null_variable is not None')

 

 

# The == operator

ifnull_variable==None:

    print('null_variable is None')

else:

    print('null_variable is not None')

 

ifnot_null_variable==None:

    print('not_null_variable is None')

else:

    print('not_null_variable is not None')

null_variable isNone

not_null_variable isnotNone

null_variable isNone

not_null_variable isnotNone

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

classMyClass:

    def__eq__(self,my_object):

        # We won't bother checking if my_object is actually equal

        # to this class, we'll lie and return True. This may occur

        # when there is a bug in the comparison class.

 

        returnTrue

 

my_class=MyClass()

 

ifmy_class isNone:

    print('my_class is None, using the is keyword')

else:

    print('my_class is not None, using the is keyword')

 

ifmy_class==None:

    print('my_class is None, using the == syntax')

else:

    print('my_class is not None, using the == syntax')

my_class isnotNone,using the iskeyword

my_class isNone,using the==syntax