Raspberry Pi: An Ultimate Walkthrough to The Raspberry Pi 3: A Complete Beginners Guide Into Starting Your Own Raspberry Pi 3 Projects by Paul Jones

Raspberry Pi: An Ultimate Walkthrough to The Raspberry Pi 3: A Complete Beginners Guide Into Starting Your Own Raspberry Pi 3 Projects by Paul Jones

Author:Paul Jones [Jones, Paul]
Language: eng
Format: epub
Published: 2017-03-05T08:00:00+00:00


>>> lst = [1, 2, 3]

>>> def foo2():

... lst += [5] # ... but this bombs!

...

>>> foo2()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<stdin>", line 2, in foo

UnboundLocalError: local variable 'lst' referenced before assignment

This will allow foo1 to run as it should do but fo2 will not. The reason for this is exactly the same as in the previous example. In this case, foo1 isn’t connected to an assignment to 1st but foo2 is. What you need to remember is that 1sr+=[f5] is the same as lst=lst+[5] – what this is doing is attempting to assign the value to 1st but the value is based on 1st and Python will assume this to be local scope – it hasn’t actually been defined.

Changing a list and iterating over it:

>>> odd = lambda x : bool(x % 2)

>>> numbers = [n for n in range(10)]

>>> for i in range(len(numbers)):

... if odd(numbers[i]):

... del numbers[i] # BAD: Deleting item from a list while iterating over it

...

Traceback (most recent call last):

File "<stdin>", line 2, in <module>

IndexError: list index out of range

If you are experienced at programming, you will know that, when you delete any item form a list or an array while you are iterating over it, you will get a problem. The above example shows a very blatant mistake but you won’t always see it so easily and even advanced developers still get tripped up by this.

Python uses a lot of different paradigms that streamline and simplify the code. One of the benefits of using simple code is that there is less chance of mistakes happening. One paradigm is called the List Comprehension paradigm, useful for avoiding the issue in the above code. Here’s how it works:

>>> odd = lambda x : bool(x % 2)

>>> numbers = [n for n in range(10)]

>>> numbers[:] = [n for n in numbers if not odd(n)] # ahh, the beauty of it all

>>> numbers

[0, 2, 4, 6, 8]

Not properly understanding how Python binds closures:

>>> def create_multipliers():

... return [lambda x : i * x for i in range(5)]

>>> for multiplier in create_multipliers():

... print multiplier(2)

...

You would expect to see this output:

0

2

4

6

8

But this is very different from what you would actually see:

8

8

8

8

8

The reason this happens is because of Python’s late binding behavior. The variable values in closures are looked up at the time the inner function is called. Every time the returned functions are called, a value of i is found within the surrounding scope. There are those who think the only solution is a hack but it isn’t. This is the solution:

>>> def create_multipliers():

... return [lambda x, i=i : i * x for i in range(5)]

...

>>> for multiplier in create_multipliers():

... print multiplier(2)

...

0

2

4

6

8

The solution is to use default arguments that will create an anonymous function that goes in your favor. Some don’t agree with this but you should understand how it works.

Circular module dependencies

Let’s assume that we have two files called a.py and b.py. Both files will import the other file:

In a.py :

import b

def f():

return b.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.