Adding static typing to Python

Bengt Richter bokr at oz.net
Thu Feb 21 02:49:57 EST 2002


On Thu, 21 Feb 2002 05:47:38 GMT, Don O'Donnell <donod at home.com> wrote:

>Fernando Pérez wrote:
>> 
>> Courageous wrote:
>> 
>> >
>> > One of the things I like about C sharp is their use of attributes,
>> > where you can define getters and setters on an attribute, but
>> > otherwise the attribute is simply used as if it were a public one.
>> > An objects attributes can thereby be used as a published aspect of
>> > its interface safely, but no one will be the wiser if you manipulate
>> > the getter and setter code.
>> 
>> Just curious, isn't there something quite similar to what you describe in
>> py2.2? I've only glanced at it so I could be wrong...
>> 
>
>
>Yes, there is, and it's not new with C#.  I think something like
>attributes with transparent getter and setter methods have been in VB at
>least since version 6.
>
IIRC Delphi's properties have worked that way since Delphi 1. I think
Anders Hejlsberg(sp?) probably brought the idea, among others, along with
him when Microsoft made him an offer he couldn't refuse and got him to
leave Borland/Inprise (or whatever it was at the time)(Not to say that
MS wasn't copying Delphi ideas before it got Anders). Delphi had a
lot of stuff first, and was/is the leader in GUI building IDEs. Plus object
pascal is compiled, and the compilation is so fast you literally don't realize
when it happenes for small (more than trivial) programs, unless you have code errors.

>I am just starting to look at the Python implementation.  It is based on
>a new built-in type called property.  In 2.2, at the interactive prompt,
>type:
>
>>>> print property.__doc__
>
ok:
--
Typical use is to define a managed attribute x:
class C(object):
    def getx(self): return self.__x
    def setx(self, value): self.__x = value
    def delx(self): del self.__x
    x = property(getx, setx, delx, "I'm the 'x' property.")
>>>
--
Then compare with an example Delphi property declaration
(snip from help file):
Here is a typical declaration for a property called Count:
--
type
  TYourComponent = class(TComponent)
 private
   FCount: Integer; { used for internal storage }
   procedure SetCount (Value: Integer); { write method }
 public
    property Count: Integer read FCount write SetCount;
 end;
--
{ curly brackets contain comments in object pascal ;-}

Note that you can specify direct access (without getter or setter)
for poperty values for either, neither, or both read and write.
Direct access is like ordinary attribute access. E.g., that way reads
can be direct and writes can make coordinated updates in setters,
as necessary (e.g. preserving shape of a rectangle when one dimension
changes, etc.). If you left out the "write SetCount" above, the
property would be read-only. You can also specify default values.

Delphi also permit array properties (declarations omitted,
snip from help file):
--
An array property is accessed by following the property
identifier with a list of actual parameters enclosed
in square brackets. For example, the statements

if Collection.Objects[0] = nil then Exit;
Canvas.Pixels[10, 20] := clRed;
Params.Values['PATH'] := 'C:\DELPHI\BIN';

correspond to

if Collection.GetObject(0) = nil then Exit;
Canvas.SetPixel(10, 20, clRed);
Params.SetValue('PATH', 'C:\DELPHI\BIN');
--

You could do worse than emulating Delphi's property stuff.

>And read Andrew Kuchling excellent "What's New in Python 2.2", Section
>2.4 Attribute Access:
>
>http://www.amk.ca/python/2.2/index.html#SECTION000340000000000000000
>
>Have fun.
>
Ok ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list