Add a feature similar to C++ "using some_namespace"

In C++, the the approach to the namespace problem is having different namespaces that should not contain different definitions of the same name. Members of a namespace can be accessed explicitly by e.g. calling "std::cout<< etc." or "using namespace std; cout<< etc." I understand Pythons approach to be "objects can be used as namespaces and their attributes are the names they contain". I find this a very beautiful way of solving the issue, but it has a downside, in my opinion, because it lacks the "using" directive from C++. If the object is a module, we can of course do "from mymodule import spam, eggs". But if it is not a module, this does not work. Consider for example: class Spam(object): def frobnicate(self): self.eggs = self.buy_eggs() self.scrambled = self.scramble(self.eggs) return self.scrambled> 42 This could be easier to implement and read if we had something like: class Spam(object): def frobnicate(self): using self: eggs = buy_eggs() scrambled = scramble(eggs) return scrambled> 42 Of course this opens a lot of conceptual questions like how should this using block behave if self doesn't have an attribute called "eggs", but I think it is worth considering.

On 2011-02-07, at 20:43 , Manuel Bärenz wrote:
In C++, the the approach to the namespace problem is having different namespaces that should not contain different definitions of the same name. Members of a namespace can be accessed explicitly by e.g. calling "std::cout<< etc." or "using namespace std; cout<< etc."
I understand Pythons approach to be "objects can be used as namespaces and their attributes are the names they contain". I find this a very beautiful way of solving the issue, but it has a downside, in my opinion, because it lacks the "using" directive from C++.
If the object is a module, we can of course do "from mymodule import spam, eggs". Or `from mymodule import *` though that's not exactly recommended.
On the other hand, since when can `using` be used on anything but a C++ namespace (which as far as I know is a well-defined entity, not an arbitrary object, and quite similar to a Python module though not identical)?
But if it is not a module, this does not work.
Consider for example:
class Spam(object): def frobnicate(self): self.eggs = self.buy_eggs() self.scrambled = self.scramble(self.eggs) return self.scrambled> 42
This could be easier to implement and read if we had something like:
class Spam(object): def frobnicate(self): using self: eggs = buy_eggs() scrambled = scramble(eggs) return scrambled> 42
Of course this opens a lot of conceptual questions like how should this using block behave if self doesn't have an attribute called "eggs", but I think it is worth considering.
My 2 cents: Javascript has this "feature" (with). It's utterly terrible, and mainly a very good way to shoot yourself in the foot repeatedly. I especially find the assertion that:
This could be easier to […] read
Extremely debatable: in my experience of that feature in Javascript, it makes code much harder to understand and reason about.

My 2 cents: Javascript has this "feature" (with). It's utterly terrible, and mainly a very good way to shoot yourself in the foot repeatedly.
I especially find the assertion that:
This could be easier to […] read Extremely debatable: in my experience of that feature in Javascript, it makes code much harder to understand and reason about. I get your point. Another downside is the uselessness of a "using"-block on a frequent problem:
class Spam(object): def frobnicate(self, egg1, egg2): self.egg1 = egg1 self.egg2 = egg2 would translate to class Spam(object): def frobnicate(self, egg1, egg2): using self: egg1 = egg1 egg2 = egg2 , which is ridiculous. However with Ethans Foxpro style suggestion, it would be ok and IMO readable.

Manuel Bärenz wrote: [...]
This could be easier to implement and read if we had something like:
class Spam(object): def frobnicate(self): using self: eggs = buy_eggs() scrambled = scramble(eggs) return scrambled> 42
Of course this opens a lot of conceptual questions like how should this using block behave if self doesn't have an attribute called "eggs", but I think it is worth considering.
In Foxpro, the keyword is 'with' instead of 'using', and to make it clear when the to look into the namespace for the variable (instead of, for example, locals), the variable name is prepended with a '.'; so the example above becomes: class Spam(object): def frobnicate(self): with self: .eggs = buy_eggs() .scrambled = scramble(eggs) return .scrambled > 42 While I have occasionally missed this feature, I haven't lost any sleep over it, either. -1 without leading periods +0 with leading periods no preference on keyword name (with vs using vs ...) ~Ethan~

Manuel Bärenz wrote:
This could be easier to implement and read if we had something like:
class Spam(object): def frobnicate(self): using self: eggs = buy_eggs() scrambled = scramble(eggs) return scrambled> 42
On the contrary, I think most people here would consider this to be *harder* to read and maintain. It seems clear to you because you just wrote it and it's fresh in your mind which namespaces the various names belong to. But to someone else, it's far from obvious, for example, whether 'scramble' is a method of self or a module-level function. It also interacts badly with Python's declaration-free determination of name scope. What if you wanted 'eggs' or 'scrambled' to be local variables rather than an attribute of self? -- Greg

Manuel Bärenz wrote:
In C++, the the approach to the namespace problem is having different namespaces that should not contain different definitions of the same name. Members of a namespace can be accessed explicitly by e.g. calling "std::cout<< etc." or "using namespace std; cout<< etc."
This is like the "with" statement of Pascal, and is already a Python FAQ: http://docs.python.org/faq/design.html#why-doesn-t-python-have-a-with-statem... -- Steven

Given that any object can be given a single character name, even as a temporary alias, this feature seems unnecessary
class Spam(object): def frobnicate(self): self.eggs = self.buy_eggs() self.scrambled = self.scramble(self.eggs) return self.scrambled
class Spam(object): def frobnicate(self): s = self # or name the parameter 's' instead of 'self' s.eggs = s.buy_eggs() s.scrambled = s.scramble(s.eggs) return s.scrambled -- Terry Jan Reedy
participants (6)
-
Ethan Furman
-
Greg Ewing
-
Manuel Bärenz
-
Masklinn
-
Steven D'Aprano
-
Terry Reedy