Where can I find string.translate source?

B

bobueland

The module string has a function called translate. I tried to find the
source code for that function. In:

C:\Python24\Lib

there is one file called

string.py

I open it and it says

"""A collection of string operations (most are no longer used).
Warning: most of the code you see here isn't normally used nowadays.
Beginning with Python 1.6, many of these functions are implemented as
methods on the standard string object. They used to be implemented by
a built-in module called strop, but strop is now obsolete itself."""

Inside the file string.py I couldn't find the source code for
translate. Where could it be?
 
M

Mike Meyer

Inside the file string.py I couldn't find the source code for
translate. Where could it be?

Object/stringmodule.c in the python source distribution.

<mike
 
F

Fredrik Lundh

The module string has a function called translate. I tried to find the
source code for that function. In:

C:\Python24\Lib

there is one file called

string.py

I open it and it says

"""A collection of string operations (most are no longer used).
Warning: most of the code you see here isn't normally used nowadays.
Beginning with Python 1.6, many of these functions are implemented as
methods on the standard string object. They used to be implemented by
a built-in module called strop, but strop is now obsolete itself."""

Inside the file string.py I couldn't find the source code for
translate. Where could it be?

in the string.py module, of course.

if you read that comment again, you'll notice that it says

many of these functions are implemented as methods on the
standard string object

and if you search for translate in string.py, you'll also find the source
code for the translate function

# Character translation through look-up table.
def translate(s, table, deletions=""):
/... docstring snipped .../
if deletions:
return s.translate(table, deletions)
else:
# Add s[:0] so that if s is Unicode and table is an 8-bit string,
# table is converted to Unicode. This means that table *cannot*
# be a dictionary -- for that feature, use u.translate() directly.
return s.translate(table + s[:0])

which calls the translate method to do the work, just as the comment
said.

to find the method implementation, you have to look at the string object
implementation. it's in the Objects directory in the source distribution.

</F>
 
B

bobueland

Thanks Mike and Fredrik. In my Python installation there is no
directory called Objects.

I use Windows and I downloaded Python from
http://www.python.org/download/

As I looked closer I saw that the link
# Python 2.4.2 Windows installer (Windows binary -- does not
include source)

which clearly says that it doesn't include source. So in order to see
the source I had to download
# Python 2.4.2 source (for Unix or OS X compile)

And in that download there is a directory called Objects and there is
file called
stringobjects.c
where one can find the implementation of translate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top