How can I force the use-only-after-declaration?

David Brown david at no.westcontrol.spam.com
Thu Dec 19 07:47:40 EST 2002


"Marco Spedaletti" <NOSPAM at NOSPAM.IT> wrote in message
news:atsbe5$1997$1 at newsreader1.mclink.it...
> 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?
>

The simple answer is no.  The complex answer is that you could make a class
and force it to give errors on unexpected attribute access, so that you can
write:
    vars = Predefined(['a', 'c']);
    vars.a = 100    # ok
    vars.b = 200    # error !
    vars.c = 300    #ok






More information about the Python-list mailing list