[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] How do assignments work?



On May 4, 2005, at 6:48 AM, Sami Hangaslammi wrote:

On 5/4/05, Dan <dan@xxxxxxxxxxxxxxxx> wrote:

In Pygame a Rect has several redundant members that can all be assigned
to. For example, you can assign to rect.topleft or rect.center, and the
effect is similar. But assigning to either affects the other, so doesn't
this require overloading the assignment operator? Python doesn't allow
that, does it?



You can't overload assignments per se, but you can intercept attribute access using __getattr__ and __setattr__ (and in newer Python versions, using properties).

pygame's Rect type uses the C API equivalent of properties..

"newer Python versions" means 2.2 and later, but only when the class derives from object. Classes with no explicit superclass are "classic" classes and don't support properties among other things.

-bob