[pypy-dev] Testing if a function exists?

Alex Gaynor alex.gaynor at gmail.com
Fri Dec 3 20:07:55 CET 2010


On Fri, Dec 3, 2010 at 2:04 PM, Dan Stromberg <drsalists at gmail.com> wrote:

> How does one test if a function exists in pypy?
>
> In CPython 2.x and 3.x, it appears to be sufficient to use:
>
>   'funcname' in __bultins__
>
> ...but that doesn't appear to work in pypy 1.4:
>
> >>>> print 'platform_version' in platform
> Traceback (most recent call last):
>  File "<console>", line 1, in <module>
> TypeError: 'module' object is not iterable
> >>>> print 'bytes' in __builtins__
> Traceback (most recent call last):
>  File "<console>", line 1, in <module>
> TypeError: 'module' object is not iterable
> >>>>
>
> TIA!
> _______________________________________________
> pypy-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/pypy-dev
>

That doesn't work for me on CPython:

alex at alex-laptop:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "bytes" in __builtins__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'module' is not iterable


You can use `hasattr` to check if a module has an attribute (function).

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you want"
-- Me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20101203/caee9603/attachment.html>


More information about the Pypy-dev mailing list