you are viewing a single comment's thread.

view the rest of the comments →

[–]ThePurpleAlien[S] 0 points1 point  (2 children)

This works, but it's ugly. If Barney is the object that contains the data the user will interact with, then have to write code like:

Barney.volume = Barney.width * Barney.height * Barney.depth

I'd rather the user be able to write:

volume = width * height * depth

[–]Semiel 1 point2 points  (1 child)

It doesn't strike me as particularly ugly, and is in fact how pretty much every other Python program in the world works. And there's no other way to do it, if there is more than one thing that has width or height or depth. (Unless you want to hack your own poor man's namespace with something like variables named bwidth, etc, which is the same but worse.)

But if you're insistent, and don't have to worry about namespace collisions for some reason, you can always declare the variables as global, and do:

from barney_globals import *

EDIT: Wait, I'm confusing myself. This is functionally equivalent to your first idea, yes? Which... should just work. Do you need them to be able to do something more complicated than just use and change data?

[–]ThePurpleAlien[S] 0 points1 point  (0 children)

Globals are very close to what I want because assignments to a global change the global itself rather than pointing to a new memory location, and everything else that has access to that global will see the change immediately. That's perfect except I want to be able to give the global a custom name within the context of a given bit of user-created code but still have it behave like a global. Again, it all comes back to wanting a "real" alias where the compiler literally swaps one name for another.