I write a function that gets a filename
, reads out information from the file and creates a Read
object out of it.
def read_file(filename):
with open(filename, 'r') as filetoread:
readList = []
for line in filetoread:
readList.append(Read(line))
return readList
This implementation works.
Now I want to generalize my function and make it work for two object/class types: Read
and Reference
.
So I want to use a class name as a parameter of a function.
The function gets a filename
and a classname
now. It reads out information from the file and creates an object of a specified classname
out of it.
My attempt lookes like this.
def read_file(filename, classname):
with open(filename, 'r') as filetoread:
readList = []
for line in filetoread:
readList.append(classname(line))
return readList
I get TypeError: 'str' object is not callable.
My idea was using this solution:
def str_to_class(classname):
return getattr(sys.modules[__name__], classname)
Source: Convert string to Python class object?
I still get an error though (TypeError: getattr(): attribute name must be string)