0

Well, i believe an example will provide a better understanding:

class FileFormats():

    def __init__(self):

        self.a_set = {gif, jpg, png}


    def add_value(self, a_value):
        self.a_set.update(a_value)

I tested and it doesn't work (and i think it's fine that it doesn't work?) I can't think of any other way to do something like that without using a '.txt' file and open(), read(), write() methods or some sort of database. But maybe there's another way i can get persistence and maybe some of you people could enlighten me?

I believe there's a very simple answer and probably i missed something on my python lessons, so, if you just tell me google this or that i would be completely fine with that.

Thank you.

Joao Guedes
  • 51
  • 1
  • 5
  • 1
    Aside from the answer given of prepending `self` before attributes and methods, note that you cannot update a set with a value. You can only update a set with an iterable. The value must be within a list or tuple. The last line in your example should be changed to `self.a_set.update([a_value])`. – joemar.ct Apr 25 '14 at 21:06
  • my mistake, i forgot to include self. in the example. – Joao Guedes Apr 25 '14 at 21:07
  • In what sense does it not work? `f = FileFormats(); f.add_value({'bmp', 'xcf'})` works fine... – unutbu Apr 25 '14 at 21:10
  • I think i expressed myself badly, i mean, it works, but when you leave interactive mode the updated values will be gone... – Joao Guedes Apr 25 '14 at 21:20
  • Oh well I was working on a answer but it seems what you want has nothing to do with what you had asked. If you want persistence for a couple of values, the easiest is to use a file. – Jacobo de Vera Apr 25 '14 at 21:24
  • You can use `pickle` or `json` to read or write data to or from file, if that's what you wanted to do. – joemar.ct Apr 25 '14 at 21:32
  • i wanted the module to write in itself so that a_set = {a} becomes a_set{a, b} after module.class.add_value((b)) – Joao Guedes Apr 25 '14 at 21:41
  • Nevermind, i will just use other files to save the values. Thanks – Joao Guedes Apr 25 '14 at 21:45

0 Answers0