A noob Python question

20 views
Skip to first unread message

Guy Adini

unread,
Sep 10, 2012, 3:48:05 AM9/10/12
to pyth...@googlegroups.com
Does anybody know how to access the list of python builtin functions from inside a function?

I am working on a distributed version of pythonect, where an XML-RPC server listens to request from the main thread, and handles the execution.
In order to do that, the server needs to wrap the python builtins, and I am trying to get to them.

In an interpreter, I can get their list through dir(__builtin__).
But inside a function, it turns out that dir() only gets the local scope.

Any ideas?


Thanks :)

Guy Adini

unread,
Sep 10, 2012, 3:55:39 AM9/10/12
to pyth...@googlegroups.com
Update:
I just saw that __builtin__ is something that isn't always available.
__builtins__ (with the s), is available though - but gives me a different result inside the start_xml_server function (definitely not the global __builtins__).
What gives?

Itzik Kotler

unread,
Sep 10, 2012, 4:33:00 AM9/10/12
to pyth...@googlegroups.com
Are you sure?

i.e.

>>> x = dir(__builtins__)
>>> def foobar():
...     return dir(__builtins__)
...
>>> x == foobar()
True
>>>

--
 
 

Guy Adini

unread,
Sep 10, 2012, 4:36:35 AM9/10/12
to pyth...@googlegroups.com
I also realized that (also trying inside a class, and getting the same results as you have - the __builtins__ stay the same).
However, when I run dir(__builtins__) inside my server.py module, I get a different list of builtins.

There's nothing in your code that could overwrite __builtins__, right? 

--
 
 

Itzik Kotler

unread,
Sep 10, 2012, 4:39:42 AM9/10/12
to pyth...@googlegroups.com
*within* Pythonect __builtins__ is extended by lang.py to provide additional functionally (i.e. expr, stmt, and remote functions).

But your code is running within Pythonect script, so I don't see why it should be any different.

Can you email me off the list the __builtins__ outputs?

Regards,
Itzik Kotler | http://www.ikotler.org

--
 
 

nir izraeli

unread,
Sep 10, 2012, 4:55:06 AM9/10/12
to pyth...@googlegroups.com
--
 
 

Itzik Kotler

unread,
Sep 10, 2012, 4:58:54 AM9/10/12
to pyth...@googlegroups.com
It seems that __builtins__ is per-module/namespace.

When running from the interpreter environment, it's equal to Python's __builtin__.

To get Pythons' builtins from within a module, do:

import __builtin__

then:

dir(__builtin__)

--
 
 

Guy Adini

unread,
Sep 10, 2012, 5:12:41 AM9/10/12
to pyth...@googlegroups.com
Thanks to both of you! 

--
 
 

Reply all
Reply to author
Forward
0 new messages