How can I force the use-only-after-declaration?
Lexy Zhitenev
zhitenev at cs.vsu.ru
Thu Dec 19 07:48:24 EST 2002
More information about the Python-list mailing list
Thu Dec 19 07:48:24 EST 2002
- Previous message (by thread): How can I force the use-only-after-declaration?
- Next message (by thread): How can I force the use-only-after-declaration?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> Hi! > > In other languages, I am used to declare variables before the use. > The translators for those languages give me an error/warning if I use a > variable that has not been declared. > > Example (C-like syntax): > > ... > > int a, c; > > a = 100; /* Ok! */ > b = 200; /* Error! */ > c = 300; /* Ok! */ > > ... > > There is a way to emulate it using Python? > In Python 2.2 you can define some slots in a class and access them. When you try to access an undefined slot, you get an AttributeError. This is is a bit tricky and classes are somewhat slower than ordinary variables. But if you need it, look here. >>> class somestruct(object): ... def __init__(self): ... self.__slots__ = ['a','c'] >>> r = somestruct() >>> r.a = 1 >>> r.c = 'Hi!' >>> r.b = ['a'] Traceback bla-bla-bla AttributeError. I could have missed some syntax formalities (I don't have Python here), but the idea is this like. Regards, Lexy.
- Previous message (by thread): How can I force the use-only-after-declaration?
- Next message (by thread): How can I force the use-only-after-declaration?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list