Search code examples
pythonpython-modulenonetype

My custom function returns the wrong thing rather than compiled string in Python


I am making the module called indeX. There is a variable __version__ which is the newest version. I have function that outputs the update info depending on the argument v. I want this function to output the latest update info if the v is 'n'. So, I have a ton of variables of type _update_desc1_0 (of course, it is not always 1_0). I have custom function _versionTransform(v) that transforms e.g. float(1.3) into str(1_3). I want to transform '_update_desc' + _versionTransform(__version__) to e.g. _update_desc1_4, and then return it.

I tried to make some boring stuff with compile() and exac(), but it didn't work.

Transformating function

def _versionTransform(v):
    if type(v) == float:
        v = str(v)
        result = v.replace('.', '_')
        return result

Version function

def getUpdate(v):
    if v == 'new' or v == 'newest' or v == 'New' or v == 'Newest' or v == 'n' or v == 'NEW' or v == \
    'NEWEST' or v == 'N':
        trans1 = _versionTransform(__version__)
        trans2 = '_update_desc' + trans1
        return exec(trans2)

I wanted it to output the info about the latest version, but it returns either '_update_desc1_3' or None


Solution

  • Oh, I solved it. The problem is that I typed return exec(trans2) instead of return eval(trans2)