[Tutor] Read the builtin module source, also itchy ellipses.
Kent Johnson
kent37 at tds.net
Fri Nov 17 15:10:28 CET 2006
Thomas wrote:
> Hi,
>
> I sometimes find it useful to read the source code of a module and for
> example I can type string.__file__ to find the location of the string
> module.
>
> However the .__file__ method is not available for the module builtin. Is
> it possible to read the source code for built in functions and if so how
> do I find them? I realise some/all may be written in C rather than
> python but it would still be interesting to read them.
It's all in C, see Python\bltinmodule.c in the source distribution.
>
> In an unrelated follow-up may I also ask: can the ellipsis (...) be used
> in code? I tried a googlecode search but none of the examples worked for
> me. Can someone post an example line of code that uses ... in it.
> This has been causing an itch in my brain, please reply with the scratch.
Syntactically ellipsis is allowed in a slice list as shown in the syntax
definition here:
http://docs.python.org/ref/slicings.html
As a practical matter I think the only containers that actually support
this kind of slicing are the arrays in Numeric and numpy. Here is a
brief example:
http://numpy.scipy.org//numpydoc/numpy-6.html#pgfId-36074
You could also support this syntax in a class of your own. Here is a
simple example that just captures the argument to __getitem__() to show
how the mechanism works:
In [4]: class Foo(object):
...: def __getitem__(self, s):
...: self.s = s
...: return None
...:
In [5]: f=Foo()
In [6]: f[...,1]
In [7]: f.s
Out[7]: (Ellipsis, 1)
Kent
More information about the Tutor
mailing list