Categories
Python Answers

How to represent an enum in Python?

Sometimes, we want to represent an enum in Python.

In this article, we’ll look at how to represent an enum in Python.

How to represent an enum in Python?

To represent an enum in Python, we can use the enum module.

For instance, we write:

from enum import Enum


class Animal(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4


print(Animal.ant)

We create the Animal class that inherits from the Enum class.

And we define the enum attributes inside the Animal class.

Therefore, Animal.ant is printed from the print function.

Likewise, we can define an enum with the Enum class directly by writing:

from enum import Enum

Animal = Enum('Animal', 'ant bee cat dog')

print(Animal.ant)

We instantiate Enum with the name of the enum and the enum attributes separated by spaces in a string.

So Animal.ant is printed from the print function.

Conclusion

To represent an enum in Python, we can use the enum module.

Categories
Python Answers

How to move a file in Python?

Sometimes, we want to move a file in Python.

In this article, we’ll look at how to move a file in Python.

How to move a file in Python?

To move a file in Python, we can use the os.rename, os.replace or shutil.move methods.

They both take the source and destination path strings as arguments respectively.

For instance, we write:

import os

os.rename("./foo.txt", "./bar.txt")

to move the file from ./foo.txt to ./bar.txt.

Likewise, we write:

import os

os.replace("./foo.txt", "./bar.txt")

or

import shutil

shutil.move("./foo.txt", "./bar.txt")

to do the same thing.

Conclusion

To move a file in Python, we can use the os.rename, os.replace or shutil.move methods.

They both take the source and destination path strings as arguments respectively.

Categories
Python Answers

How to check if a variable exists with Python?

Sometimes, we want to check if a variable exists with Python.

In this article, we’ll look at how to check if a variable exists with Python.

How to check if a variable exists with Python?

To check if a variable exists with Python, we can use the locals function to check if a local variables exists.

We can use the globals function to check if a global variable exists.

And we can use hasattr to check if an object has the given attribute.

For instance, we write:

bar = 1


def baz():
    foo = 1
    if 'foo' in locals():
        print('foo exists')


baz()

if 'bar' in globals():
    print('bar exists')


class A:
    attr = 1


obj = A()
if hasattr(obj, 'attr'):
    print('attr exists')

We have the bar global variable.

And we have the baz function with the foo local variable.

In baz, we check if foo is in baz using if 'foo' in locals().

We check if bar is a global variable that’s defined with if 'bar' in globals().

Also, we have an A class with the attr attribute.

We instantiate that and assign the A instance to obj.

Then we check if attr is in obj with if hasattr(obj, 'attr').

Since all of the exist, we should see:

foo exists
bar exists
attr exists

printed.

Conclusion

To check if a variable exists with Python, we can use the locals function to check if a local variables exists.

We can use the globals function to check if a global variable exists.

And we can use hasattr to check if an object has the given attribute.

Categories
Python Answers

How to reverse a list in Python?

Sometimes, we want to reverse a list in Python.

In this article, we’ll look at how to reverse a list in Python.

How to reverse a list in Python?

To reverse a list in Python, we can use the reversed function.

For instance, we write:

array = [0, 10, 20, 40]
l = list(reversed(array))
print(l)

We call reversed with array to return an iterator with the entries in array reversed.

Then we convert that back to a list with list.

Therefore, l is [40, 20, 10, 0].

Conclusion

To reverse a list in Python, we can use the reversed function.

Categories
Python Answers

How to remove duplicates in lists in Python?

Sometimes, we want to remove duplicates in lists in Python.

In this article, we’ll look at how to remove duplicates in lists in Python.

How to remove duplicates in lists in Python?

To remove duplicates in lists in Python, we can convert the list to a set and then back to a list with the set and list functions respectively.

For instance, we write:

t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
s = list(set(t))
print(s)

We call set with t to return a set with t‘s elements but without the duplicates.

Then we call list to convert the set back to a list and assign it to s.

Therefore, s is [1, 2, 3, 5, 6, 7, 8].

Conclusion

To remove duplicates in lists in Python, we can convert the list to a set and then back to a list with the set and list functions respectively.