top of page
  • Writer: VC Healy
    VC Healy
  • Jul 26, 2020
  • 1 min read

Adjoining iterables into one iterable object


The chain module is imported from the itertools library.

from itertools import chain
# Two lists (iterable objects)
fruit = ['apple ', 'banana ', 'strawberry ']
amount = ['1' ,'2' , '3']

Combining them into a single list with the following

combo = list(chain(fruit, amount))
print(combo)

combo will attach the lists in the order received in the command. The order of the content of each list will not be altered.

['apple ', 'banana ', 'strawberry ', '1', '2', '3']

In the above example, a new list was created but replacing the keyword list with set or tuple will give the corresponding object type.

 
 
  • Writer: VC Healy
    VC Healy
  • Jul 26, 2020
  • 2 min read

A nested scope/function is where a function is held within another function. The inner function could be making use of free variables in the outer function as part of its routine.

def main_function():
    msg = 'Hello' # Free Variable
    def inner_function():
        print (msg) # Request for Free Variable
    inner_function()
    print (msg)
    
main_function()

The order that the commands are inside a function matters.

In the following, the msg variable can be seen in two locales.

The main_function and the inner_function.

msg of the inner_function is not seen by the main_function. The inner_function msg is a local variable. When it goes to print, the inner variable is assigned the value of the free variable and the inner_function does not look outside the main_function.


The first print in this function will be actioned by the inner_function() and the main_function print statement will action after


def main_function():
    msg = 'Vincent'
    def inner_function():
        msg = 'Hello'
        print (msg)
    inner_function()
    print(msg)

main_function()

The following code shows another possible way of changing one of the variables.

msg is initial given the value 'Vincent' as the main_function starts. As it progresses into the inner_function, the non-local msg commands then states that msg is a variable outside of the function. This means when the variable msg is changed to 'Hello' by the inner_function it changes the value outside the inner function as well.


Both the main_function and the inner_function will print the same value of 'Hello'

def main_function():
    msg = 'Vincent'
    def inner_function():
        nonlocal  msg
        msg = 'Hello'
        print (msg)
    inner_function()
    print(msg)

main_function()


Instead of the function printing out values to the console the inner_function is returning a callable value to the main_function. The main_function that is callable is then assigned to a variable object. This allows the variable to be treated as the main_function.

def main_function():
    msg = 'Mars Attacks'
    def inner_function():
        print (msg)
    return inner_function

object = main_function()

Adding the parenthesis to the object will action the main_function and print the msg.



object()
Mars Attacks

 
 
  • Writer: VC Healy
    VC Healy
  • Jul 26, 2020
  • 1 min read

Haven't written much in a bit for various reasons.


Hopefully, this is me back on track

Associative array in Python invariably means a form of a dictionary. Behind most objects there is a dictionary. It is the link between a key-value pair.

A symbol(variable) given string value is a key-value pair and a collection of these would give you a dictionary. A tuple of key-value pairs would be considered this.

The DDP Part 3 course goes into how these are handled by Python.

The hash of maps will ensure values that are the same will have the same hash value, although for security reasons the hash value(integer) could be different for each run of the code. Equivalent values would always be equal though.


It is an interesting topic and something I will look to review again in the coming days.



 
 

© 2020 by Vincent Healy. Proudly created with Wix.com

bottom of page