Home > database >  Jython - Do these two boolean statements do the same thing?
Jython - Do these two boolean statements do the same thing?

Time:03-06

I'm reasonably new to Python but I thought I understood how the flow control worked.

I'm pasting this from the Jython github at line 418

418: pkgname = globals.get('__package__')
419: if pkgname is not None:
420:     if not pkgname and level > 0:
421:         raise ValueError, 'Attempted relative import in non-package'

Does pkgname is not None on line 419 do the same thing as not pkgname on line 420? If it does, why would that check be there?

I'm reading the import source code so I can understand the system better and, though minor, this struck me as odd. As I'm still learning Python, I don't want to take anything for granted.

Thank you!

CodePudding user response:

I don't know what the valid values are for pkgname but technically not pkgname could test for any falsy value, such as None, but also False, 0, [], "" etc. However, globals.get() will only return None if the globals() dict key is absent so if the intention is only to test for that then it is indeed redundant.

Side note: wonder why you're learning python via reading the Jython codebase. I suggest you start with using the reference CPython implementation and with the docs rather than the codebase itself

CodePudding user response:

You are correct the second check for if not pkgname is not needed so the code can just be

418: pkgname = globals.get('__package__')
419: if pkgname is not None:
420:     if level > 0:
421:         raise ValueError, 'Attempted relative import in non-package'
  • Related