[Tutor] creating read only attributes

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Nov 22 04:10:56 EST 2003


> I've been looking at some Delphi recently, and you can create 
> read only attributes, which behave like variables. 
> consider the following code:
>
> type TTest = class
> private
>     testvar = integer;
> public
>     property Test: integer read testvar;
>     constructor Create;
> end;

> I'm sure there's a way to do this in python, 
> but I haven't found it yet.

Python recently borrowed the idea of properties from Delphi.
Check the docs for how to declare a "property" in Python
(introduced in v2.2 I think). Its not identical to 
Delphi but very close.

Having said that I've never used them. The extra work 
needed to create them isn't worth the payback. The reason 
Delphi has them is primarily to allow you to expose your 
own object attributes within Delphi's GUI builder. 
The Delphi Property Inspector will only show attributes 
declared as properties. And will only allow you to change 
properties with a write mechanism defined.

Since Python doesn't have such an IDE requirement I've 
never seen a need for Properties in Python, except to 
save a couple of keystrokes.

Alan G.



More information about the Tutor mailing list