I found a way that could make it compact:
```
from petsc4py import PETSc

file_format = 'hdf5'

# instance of PETSc viewer
v = PETSc.Viewer()

# make a list with attributes in object
viewer_attr = dir(v)

# search for the index and get attr
try:
	create_viewer = getattr(v,viewer_attr[map(str.lower,viewer_attr).index('create'+file_format)])
	# for example only print name of method in create_viewer
	print create_viewer.__name__
except:
	msg = 'file format %s not recognized by io handler petsc' % file_format
	raise IOError(msg)
```