Editing Python Source Code

A forum for general discussion of the Python programming language.

Editing Python Source Code

Postby cgs » Mon Nov 04, 2013 4:25 am

Hi,
I would like to change the list function len to having the name lend in the python source code. That way, instead of len(list) I could run lend(list)
How would I go about this. I'm running Python 2.7.5 on Mac OS 10.9.

Thanks
cgs
 
Posts: 1
Joined: Mon Nov 04, 2013 4:22 am

Re: Editing Python Source Code

Postby micseydel » Mon Nov 04, 2013 6:39 am

The simplest way would be to put "lend = len" at the top of your Python files, although I'm not sure why you'd want to do that. Otherwise you could modify the Python interpreter, written in C. Is that what you want to do?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Editing Python Source Code

Postby Kebap » Mon Nov 04, 2013 1:34 pm

cgs wrote:Hi,
I would like to change the list function len to having the name lend in the python source code. That way, instead of len(list) I could run lend(list)
How would I go about this. I'm running Python 2.7.5 on Mac OS 10.9.

Thanks

Hi cgs and welcome! This is a strange request. Please read my signature on how to ask smarter questions. Here is an answer however:

Code: Select all
def lend(arg):
  return len(arg)


That way, lend(list) would also return the results expected by len(list).
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Editing Python Source Code

Postby Mekire » Mon Nov 04, 2013 2:25 pm

Kebap wrote:Here is an answer however:

Code: Select all
def lend(arg):
  return len(arg)

That way, lend(list) would also return the results expected by len(list).

As Micseydel already pointed out, all you would need to do is:
Code: Select all
lend = len
Code: Select all
>>> lend = len
>>> lend([1,2,3,4])
4

-Mek
User avatar
Mekire
 
Posts: 1008
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Editing Python Source Code

Postby Kebap » Mon Nov 04, 2013 2:51 pm

Mekire wrote:As Micseydel already pointed out, all you would need to do is:

That may be true, but I find my solution more readable. Also, maybe the OP is a python beginner who can use this example to understand python functions better, and define functions of her own.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: W3C [Linkcheck], Yoriz and 3 guests