Varable parsing error with python

Dave Angel davea at davea.name
Tue Feb 10 09:37:57 EST 2015


On 02/10/2015 03:29 AM, OmPs wrote:
> On 10 Feb 2015 13:12, "Chris Angelico" <rosuav at gmail.com> wrote:
>>
>> On Tue, Feb 10, 2015 at 6:30 PM, OmPs <torque.india at gmail.com> wrote:
>>>      def _getPackgeVersion(xmlfile, p):
>>>          package = str(p)
>>>          if isinstance(fpmdict["application"]["package"], list):
>>>              for i in fpmdict["application"]["package"]:
>>>                  if i["@name"] == p:
>>>                      _pkgVersion = i["version"]
>>>          else:
>>>              _pkgversion = fpmdict["application"]["package"]["version"]
>>>              return _pkgVersion
>>
>> One of your branches doesn't have a return statement in it, so Python
>> just returns None. You may want to unindent that return statement one
>> level.
>>
> Tried that as well getting the same error.

The code above has at least two errors.  Fix them both, post a new 
version, and tell us the error you now get.

First error is that there's no return statement for the case where the 
first if is true.  You might fix that by unindenting, or by adding 
another return somewhere, or whatever.

Second error, depending on how you fixed the first one, is that for some 
paths through the function, _pkgVersion isn't initialized.  So if those 
conditions happen, and if you fixed the first error, you'll get an 
exception like:

UnboundLocalError: local variable '_pkgVersion' referenced before assignment

There also may be logic errors.  For example, if there are multiple 
places where the if i["@name...   logic triggers, do you want the 
_pkgversion to be the first such, or the last such.  What do you want 
for a value if there no matches?




-- 
DaveA



More information about the Python-list mailing list