Pre-PEP ideas

Richard Jones rjones at ekit-inc.com
Sun Feb 16 21:17:50 EST 2003


On Mon, 17 Feb 2003 1:06 pm, Steven Cummings wrote:
> 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

Note: you can't use "print" because it's a keyword.

I'd vote -1 for this simply because there's already a way to do this.

Python 1.5.2 (#1, Jul  5 2001, 03:02:19)  [GCC 2.96 20000731 (Red Hat Linux 
7.1 2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class A:
...     def __init__(self, value=None):
...         self._value = value
...
>>> def show(self):
...     print self._value
...
>>> A.show = show
>>>
>>> a = A('hello, world')
>>> a.show()
hello, world
>>>


    Richard






More information about the Python-list mailing list