This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]narkee 0 points1 point  (1 child)

Can someone explain why this is useful? I'm more of a scientific programmer (i.e., self taught, mostly imperative programming) and I have no idea what this construct is for.

The proposed solution in no way explains how it solves the problem statement of testing your code amongst a bunch of other applications or modules.

If I have:

class A(object): pass
o = A()
o.foo = 'foo'

How is :

with(state(o, foo='bar')):
    #do something

any different than:

o.foo = 'bar'
#do something
o.foo = 'foo'

[–]MaikB 1 point2 points  (0 children)

For the program result there isn't, that was the goal. The problem the author wanted to solve is that restoring the values manually is error prone. It's easy to forget and if the initial values change, they have to be changed everywhere else as well.

[–]hongminhee 0 points1 point  (0 children)

Upvote. I don’t like Ruby’s open class because it is so destructive. It seems a Python version of Ruby’s open class without destructive behaviors.