Skip to main content

Hi,

I'm programming in Python using FME Desktop, specifically using the Python Caller transformer. I'm trying to use a variable called test in my script, where test should have the value assigned to the attribute _creation_instance. I'm declaring it as follows: test = feature.getAttribute('_creation_instance'), but I'm getting the following error:

Python Exception <NameError>: name 'feature' is not defined

 

How can I properly get the value from _creation_instance?

 

Thank´s

Can you post the entire contents of the PythonCaller using the Code option in the Reply box?

 


Be good to see your complete code block from the PythonCaller, but looking at the error you probably have your code in the wrong function. You need to have it in the input function under the FeatureProcessor class

import fme
import fmeobjects


class FeatureProcessor(object):

def __init__(self):
pass

def input(self, feature):



test = feature.getAttribute('_creation_instance')



self.pyoutput(feature)

def close(self):
pass

def process_group(self):
pass

def has_support_for(self, support_type):
if support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM:
return False
return False

 


..

Python Exception <NameError>: name 'feature' is not defined

 

This means that feature has not been declared as an FMEFeature variable within the Function block it is being called.

As @hkingsbury says, most use cases feature is called from def input(self, feature) where feature is  declared in the function statement and does not need to be declared separately…...so the feature.getAttribute('_creation_instance') is likely in the wrong function block if you intent for it to execute within the input() function ie. Will only get this error if instead say, try to call feature.getAttribute('_creation_instance') inside def close() etc.  where as feature is not an input into this function and so does not pass into the function as an FMEFeature, then just calling “feature” within this Python will be trying to call a method on something that hasn’t locally been instantiated as an FMEFeature

If you instead want to make feature a separate FMEFeature that is not the same feature incoming to input() then this has to be declared either locally or globally as feature = fmeobjects.FMEFeature() to call the instantiation of feature as an FMEFeature object.


Reply