osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

newb question about @property


On Thu, 5 Oct 2017 09:08 am, bartc wrote:

[...]
> And when I tried, it didn't really work in Python 2 (extra attributes
> could still be created, and .__slots__ wasn't readonly); only Py3.

Not quite, but I don't blame you for the mistake. Its an easy one to make.

__slots__ only works in "new style classes", not "classic classes" in Python
2. And yes, it is a wart in Python 2 that there are two subtly different
kinds of class. This is due to historical reasons, and its fixed in Python 3.

In Python 2, "classic classes" are declared like this:

class MyClass:
    ...

(or by inheriting from another classic class).

New-style classes, or "types", inherit from object:

class MyClass(object):
    ...


(or some other new-style type, like int, dict, list, etc.)

Yes yes yes, I completely agree that this is a suboptimal situation. It is a
language wart and a trap for the beginner, or even the experienced coder. Use
Python 3, where it is fixed.





-- 
Steve
?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure
enough, things got worse.