create new class using existing variable previously used for same class - python

Question

I'm doing something like below and confused at the results. I've tried looking searching for how to accomplish this but can't seem to find anything. Why in the second block is ec.exs still test ? How can I properly re-use the ec variable for a new instance of that class ?

class ExampleClass(object):
    def __init__(self, ex="", exs=set()):
        self.ex = ex
        self.exs = exs

ec = ExampleClass(ex="hello")
ec.exs.add("test")
print ec # <__main__.ExampleClass object at 0x0000000002F51668>
print ec.ex # hello
print ec.exs # test

ec = ExampleClass(ex="goodbye")
print ec # <__main__.ExampleClass object at 0x0000000002F609E8>
print ec.ex # goodbye
print ec.exs # Why is this still "test" ?

Show source
| class   | python   | instance   2017-01-07 01:01 0 Answers

Answers to create new class using existing variable previously used for same class - python ( 0 )

Leave a reply to - create new class using existing variable previously used for same class - python

◀ Go back