Categories
Python Answers

How to use global variables between files in Python?

Sometimes, we want to use global variables between files in Python.

In this article, we’ll look at how to use global variables between files in Python.

How to use global variables between files in Python?

To use global variables between files in Python, we can use the global keyword to define a global variable in a module file.

Then we can import the module in another module and reference the global variable directly.

For instance, we write:

settings.py

def init():
    global myList
    myList = []

subfile.py

import settings


def stuff():
    settings.myList.append('hi')

main.py

import settings
import subfile

settings.init()
subfile.stuff()
print(settings.myList[0])

We import the settings and subfile modules in main.py.

Then we call settings.init to create the myList global variable and assign it to an empty array.

Then we call subfile.stuff to call settings.myList.append to add an entry to the settings.myList global variable.

Then we print the value of settings.myList[0], which is 'hi'.

Conclusion

To use global variables between files in Python, we can use the global keyword to define a global variable in a module file.

Then we can import the module in another module and reference the global variable directly.

Categories
Python Answers

How to generate the Fibonacci Sequence with Python?

Sometimes, we want to generate the Fibonacci Sequence with Python.

In this article, we’ll look at how to generate the Fibonacci Sequence with Python.

How to generate the Fibonacci Sequence with Python?

To generate the Fibonacci Sequence with Python, we can create a generator function that yields the value the sequence.

For instance, we write:

def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b


for index, fibonacci_number in zip(range(10), fib()):
    print(index, fibonacci_number)

We create the fib function that uses yield to return the Fibonacci sequence by assigning b to a and b to a + b.

Then we use a for loop that zips range(10) and the iterator returned by the fib function together to generate the first 10 Fibonacci sequence values.

In the loop body, we print the index and fibonacci_number values.

Therefore, we see:

0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34

logged.

Conclusion

To generate the Fibonacci Sequence with Python, we can create a generator function that yields the value the sequence.

Categories
Python Answers

How to calculate the Euclidean distance with Python NumPy?

Sometimes, we want to calculate the Euclidean distance with Python NumPy.

In this article, we’ll look at how to calculate the Euclidean distance with Python NumPy.

How to calculate the Euclidean distance with Python NumPy?

To calculate the Euclidean distance with Python NumPy, we can use the numpy.linalg.norm method.

For instance, we write:

import numpy

a = numpy.array((1, 2, 3))
b = numpy.array((4, 5, 6))

dist = numpy.linalg.norm(a - b)
print(dist)

We create 3 NumPy arrays a and b.

Then we call numpy.linalg.norm with a - b to calculate the distance between points a and b.

Therefore, dist is 5.196152422706632.

Conclusion

To calculate the Euclidean distance with Python NumPy, we can use the numpy.linalg.norm method.

Categories
Python Answers

How to get the source code of a Python function?

Sometimes, we want to get the source code of a Python function.

In this article, we’ll look at how to get the source code of a Python function.

How to get the source code of a Python function?

To get the source code of a Python function, we can use the inspect.getsource method.

For instance, we write:

import inspect


def foo(arg1, arg2):
    a = arg1 + arg2
    return a


lines = inspect.getsource(foo)
print(lines)

We create the foo function that we want to get the code for into a string.

To do that, we call inspect.getsource with foo and assign the code string to lines.

Therefore, lines is:

def foo(arg1, arg2):
    a = arg1 + arg2
    return a

Conclusion

To get the source code of a Python function, we can use the inspect.getsource method.

Categories
Python Answers

How to sort a list by multiple attributes with Python?

Sometimes, we want to sort a list by multiple attributes with Python.

In this article, we’ll look at how to sort a list by multiple attributes with Python.

How to sort a list by multiple attributes with Python?

To sort a list by multiple attributes with Python, we can use the sorted function with the operator.itemgetter method.

For instance, we write:

import operator

l = [[12, 'tall', 'blue', 1], [2, 'short', 'red', 9], [4, 'tall', 'blue', 13]]
s = sorted(l, key=operator.itemgetter(1, 2))
print(s)

We have the l nested list that we want to sort by the 2nd and 3rd items in each nested list.

Then we call sorted with l and the key set to operator.itemgetter called with 1 and 2 to sort l and return the sorted list.

We then assign the returned list to s.

Therefore, s is [[2, 'short', 'red', 9], [12, 'tall', 'blue', 1], [4, 'tall', 'blue', 13]].

Conclusion

To sort a list by multiple attributes with Python, we can use the sorted function with the operator.itemgetter method.