How to get/set class attributes in Python
Chris Spencer
usenet.20.evilspam at spamgourmet.com
Sun Jun 12 10:40:26 EDT 2005
Kalle Anke wrote:
> On Sun, 12 Jun 2005 13:59:27 +0200, deelan wrote
> (in article <jrVqe.16350$TR5.11328 at news.edisontel.com>):
>
> void doSomething( data : SomeClass ){ ... }
>
> and I would be sure at compile time that I would only get SomeClass objects
> as parameters into the method.
Being an untyped language, Python does not require you to enforce types.
However, for those that require such functionality, you can get away
with using the "assert" statement. For example, if I wanted to make sure
my function foo was only given instances of class Bar, I'd write
something like:
>>> class Bar: pass
>>> def foo(bar):
... assert isinstance(bar, Bar), "argument is not of type Bar"
... print "argument must be of type Bar"
...
>>> bar = Bar()
>>> foo(bar)
argument must be of type Bar
>>> foo(123)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in foo
AssertionError: argument is not of type Bar
>>>
Chris
More information about the Python-list
mailing list