python3 string format

Ian Kelly ian.g.kelly at gmail.com
Tue Mar 26 11:07:13 EDT 2013


On Tue, Mar 26, 2013 at 4:51 AM, Shiyao Ma <i at introo.me> wrote:
> Thx for your reply.
> I am using pycharm and simply press "go to declaration" which directs me to
> a py file, containing the following code:
> def format(*args, **kwargs): # known special case of str.format
>         """
>         S.format(*args, **kwargs) -> string
>
>         Return a formatted version of S, using substitutions from args and
> kwargs.
>         The substitutions are identified by braces ('{' and '}').
>         """
>         pass

I would guess that Python declaration is maintained and used by
PyCharm for code intelligence.

> I am curious how you find the corresponding c source code.

The str object is mostly implemented in Objects/unicodeobject.c.  The
int object is mostly implemented in Objects/longobject.c.  Other
built-in types can also be found in that directory.  Each has a
PyMethodDef[] global array that declares the implementations of the
object's methods.  Finding the implementation is then just a matter of
grepping the source for its name.

The function I pointed you to before implements the str.__format__
method.  The str.format method itself is at:

http://hg.python.org/cpython/file/84e73ace3d7e/Objects/stringlib/unicode_format.h#l942

I'm not sure why that one is implemented in a .h file.



More information about the Python-list mailing list