in statement and namespace type ideas

Roman Suzi rnd at onego.ru
Sat Jun 16 16:24:25 EDT 2001


Hello,

right now namespace handling in Python uses dicts,
however, docs say it could change.

I can't be sure that my code which uses globals() to merge, for example,
cgi-form variables with globals namespace will work with future Python
versions. There are other examples where I need to transfer some
variables from one namespace to another. Dict is good type for namespace,
but maybe it's time to propose new mapping type - namespace -
to do things I described?

I also thought about absent "with" operator and how it could be used
in Python. Maybe it is beneficial to have "with" syntax like in the
followin example:

class Example:
  a = 1
  b = 2
  def aaa(self):
    k = 3
  def bbbb(self):


e = Example()
# 1

in e.__namespace__:  # temparary change locals() with Example.__dict__
  c = 3
  aaa()
  print k

# or 2:

in e:
  c = 4

This will be almost equivalent to:

exec """
  c = 3
 ...

""" in e.__dict__

except it will be compiled at compile-time.

The problem I see is that #1 is too verbose and #2 is not
always useful. For example, if I need e to be mapping type
object, I will assume that

in e:
  ...

will mean "in namespace, which e defines" not "in namespace of e".

This could, of course, be resolved, for example, if namespace type
will have some attribute, say, __i_am_namespace__.

It could also be arranged that namespaces could be merged like that:

in e+local():           # locals() has more priority
  ...

or

in locals()+e:          # e overrides locals()

and so on. (Syntax could be different).

in-statement could be useful inside method defs:

class AAA:
  _delta = 1
  def met(self, a, b, c):
    in vars()+self:
      print a + _delta
      return b + c + _delta


"in" is in kwlist  already, though, no problem here.
in could be called "temporary namespace application".

I even think, in-statement could be used in the mode of
"with" (as in Pascal), that is, always:

in locals()+e, globals()+g:
  ...

by writing only:

in e, g:
  ...

But I think my scheme is more explicit.

Or, maybe, it is possible to do it this way:

in additional, locals(), globals():
  ...

with all new variables considered in "additional" namespace?

* * *

Are these ideas worth serious consideration?

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Saturday, June 16, 2001 _/ Powered by Linux RedHat 6.2 _/





More information about the Python-list mailing list