By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

overiding assignment in module

P: n/a
Hello.
I would to do something like this.

container = []

p1 = point()
l1 = line()

and i would like to override = method of the module so that its puts
all objects into container.
how i can do something like that.
thanks for help,
viktor.

Oct 25 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Viktor Marohnic wrote:
I would to do something like this.

container = []

p1 = point()
l1 = line()

and i would like to override = method of the module so that its puts
all objects into container.
how i can do something like that.


you cannot, at least not as you've described the problem. assignment is not
an operator in Python; it's a statement that modifies the current namespace.

things you can do include

- use an explicit target object

x.p1 = point()
x.l1 = line()

- execute the script in a controlled fashion, and inspect the resulting namespace:

myscript = """
p1 = point()
l1 = line()
"""

# create a new namespace, and add "built-in" functions to it
namespace = {}
namespace["point"] = point
namespace["line"] = line

# execute the script in this namespace
exec myscript in namespace

for key, item in namespace.iteritems():
...

</F>

Oct 25 '05 #2

P: n/a
On Tue, 25 Oct 2005 04:56:02 -0700, Viktor Marohnic wrote:
Hello.
I would to do something like this.

container = []

p1 = point()
l1 = line()
Choosing names that look like numbers is terrible practice. Don't use l,
l1, O, ll, and so forth, unless you are trying to deliberately make your
code hard to read, hard to understand, and easy to miss bugs.

and i would like to override = method of the module so that its puts
all objects into container.
What do you mean by put all objects into container?

Do you mean:

container = []
container = point()
container = line()

Or do you mean:

container = []
container.append(point())
container.append(line())

Or even:

container = []
container.insert(0, point())
container.insert(0, line())

Or possibly even:

container = []
container.extend(point())
container.extend(line())
how i can do something like that.


Choose a different language.

There is no assignment method in Python.
--
Steven.

Oct 25 '05 #3

P: n/a
Ok thanks a lot. I think i got the point.
I also thought that it could be possible to do something like this
globals().__setitem__ = custom_setter
but __setitem__ is readonly

Oct 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.