Pre-PEP ideas

Jp Calderone exarkun at intarweb.us
Mon Feb 17 03:24:57 CET 2003


On Sun, Feb 16, 2003 at 09:06:27PM -0500, Steven Cummings wrote:
> Hello!
> 
> I've got a couple of ideas for the Python language that I wanted to run by
> this list before I wrote up a couple of pre-PEP's.  The guidelines say
> that I should generate discussion here or they will be rejected outright,
> so here goes nothing.
> 
> 
> 1. Separate Definition (or Dynamic Addition) of Methods
> 
> As we all know and love Python it provides many facilities for run-time
> checking and typing, and for many things to be defined on the fly.  For
> example, given an object "o", its class can be dynamically changed by
> making the statement "o.__class__ = C" where C is a class other than that
> originally used to initialize "o".
> 
> My specific idea is to allow the definition of methods outside of the
> class's main definition.  This would allow a class to be dynamically
> constructed based on conditional constructs.  Suppose the initial
> definition of a class were the following.
> 
> class A(object):
>     "my simple example class"
>     def __init__(self, value=None):
>         "create an instance of A"
>         self._value = value
> 
> Now suppose that we could add methods like the following.
> 
> def A.print(self):
>     "print a string representation to sys.stdout"
>     print self._value
> 
> This syntax could allow for the "self" argument to be omitted in cases
> where the coder would like the method to be specified as a static- or
> class-method later on. Otherwise, that's the basic gist of this idea. I
> thought it could make python a little more dynamic to be able to add
> methods like this on the fly. I don't have a lot of details here as you
> see, so perhaps someone on this mailing list can identify potential
> problems or issues. I would also like to identify potential practical uses
> as right now it's only an idea for making Python more dynamic in my mind.
> 

  A form of this is available with no modifications to Python.  See
http://www.intarweb.us:8080/evil/reopenable.py for an example of how.

> 
> 2. Alternative String Interpolation Syntax
> 
> [snip - string interpolation proposal]
> 
> Now on to the expressions. I think it would be reasonable to allow simple
> expressions inside of the parentheses as well. So for numbers, a coder
> could write "%(x + y)d" or "%(x + y)f" and get the numeric results
> inserted into the result appropriately given the type.

  Unfortunately, "x + y" is a perfectly valid dictionary key.

  print "%(x + y)d" % {'x + y': 10}

  What about %(expr)E as an entirely -new- format specifier ("E" standing
for "Expression", of course)?  The contents of the () could be evaluated
using the provided dictionary as the local namespace, the result str() or
repr()'d, and dropped in place.  I haven't followed previous debates on this
topic though, so maybe it's been proposed and shot down. ;)

  Jp

-- 
There are 10 kinds of people: those who understand binary and those who do
not.
-- 
 up 8 days, 6:28, 3 users, load average: 0.05, 0.04, 0.00
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030216/d2991186/attachment.pgp>


More information about the Python-list mailing list