Categories
Python Answers

How to determine the type of an object with Python?

Sometimes, we want to determine the type of an object with Python.

In this article, we’ll look at how to determine the type of an object with Python.

How to determine the type of an object with Python?

To determine the type of an object with Python, we can use the type function.

For instance, we write:

class Test1 (object):
  pass
class Test2 (Test1):
  pass

a = Test1()
b = Test2()
print(type(a) is Test1)
print(type(b) is Test2)

We have 2 classes, Test1 and Test2.

And we create an instance of each and assign it to a and b respectively.

Then we call type function with a and b to check if it’s an instance of Test1 and Test2 respectively.

Therefore, we see that both are True.

Conclusion

To determine the type of an object with Python, we can use the type function.

Categories
Python Answers

How to read a file line-by-line into a list with Python?

Sometimes, we want to read a file line-by-line into a list with Python.

In this article, we’ll look at how to read a file line-by-line into a list with Python.

How to read a file line-by-line into a list with Python?

To read a file line-by-line into a list with Python, we can use the use the with statement and the open function.

For instance, we write:

foo.txt

foo
bar
baz
filename = './foo.txt'
with open(filename) as file:
    for line in file:
        print(line.rstrip())

We set filename to the path of foo.txt.

Then we call open with filename to read the file into file in the with statement.

Then we loop through each line in the file.

And we call line.rstrip to trim the whitespaces at the end of each line.

Since we use the with statement, the file will be closed automatically after we’re done reading it.

Therefore, we see:

foo
bar
baz

printed.

Conclusion

To read a file line-by-line into a list with Python, we can use the use the with statement and the open function.

Categories
Python Answers

How to check if an object has an attribute in Python?

Sometimes, we want to check if an object has an attribute in Python.

In this article, we’ll look at how to check if an object has an attribute in Python.

How to check if an object has an attribute in Python?

To check if an object has an attribute in Python, we can use the hasattr function.

For instance, we write:

class A:
  property = 'foo'

a = A()  
if hasattr(a, 'property'):
    print(a.property)

We have an A class with the property instance property set to 'foo'.

Then we create a new instance of A.

And then we call hasattr with a and 'property' to check if property property exists in a.

Since this is True, we see 'foo' printed.

Conclusion

To check if an object has an attribute in Python, we can use the hasattr function.

Categories
Python Answers

How to call a function of a module by using its name string with Python?

Sometimes, we want to call a function of a module by using its name string with Python.

In this article, we’ll look at how to call a function of a module by using its name string with Python.

How to call a function of a module by using its name string with Python?

To call a function of a module by using its name string with Python, we can use the getattr function.

For instance, we write:

foo.py

def bar():
  print('bar')

Then we write:

import foo
method_to_call = getattr(foo, 'bar')
method_to_call()

We import the foo module with import foo.

Then we call getattr with the module and the name of the function we want to retrieve.

We assign the returned function to method_to_call.

Finally, we call method_to_call.

Therefore, we see 'bar' printed.

Conclusion

To call a function of a module by using its name string with Python, we can use the getattr function.

Categories
Python Answers

How to get the number of elements in a list with Python?

Sometimes, we want to get the number of elements in a list with Python.

In this article, we’ll look at how to get the number of elements in a list with Python.

How to get the number of elements in a list with Python?

To get the number of elements in a list with Python, we can use the len function.

For instance, we write:

print(len([1, 2, 3]))

then we see 3 printed since our list has 3 items.

Conclusion

To get the number of elements in a list with Python, we can use the len function