1

I'm tasked to make a "Set" class that contains the variable self.list and be able to print and str() the object by writing the __repr__ and __str__ methods. A second file (driver1.py), a "driver file" creates a Set object and attempts to call print(str(set_object)) and print(set_object) but both calls only print a memory address, Set.Set instance at 0x1033d1488> or some other location. How do I change this? I want it to print out the contents of the set_object in the form {1,2,3}

Here is my code in it's entirety after updating indentation.

class Set:

def __init__(self):
    self.list = []

def add_element(self, integer):
    if integer not in self.list:
        self.list.append(integer)

def remove_element(self, integer):
    while integer in self.list: self.list.remove(integer)

def remove_all(self):
    self.list = []

def has_element(self, x):
    while x in self.list: return True
    return False
#probably doesnt work, __repr__
def __repr__(self):
    if self.list.len == 0:
        return "{}"
    return "{"+", ".join(str(e) for e in self.list) +"}"
#Same as above, probably doesnt work
def __str__(self):
    if len(self.list) == 0:
        return "{}"
    return "{"+", ".join(str(e) for e in self.list) +"}"

def __add__(self, other):
    counter = 0
    while counter <= len(other.list):
        if other.list[counter] not in self.list:
            self.list.append(other.list[counter])
        counter = counter + 1

Why do I get the error:

 Traceback (most recent call last):
  File "driver1.py", line 1, in <module>
    from Set import *
  File "/Users/josh/Documents/Set.py", line 23
    return "{"+", ".join(str(e) for e in self.list) +"}"
                                                       ^
IndentationError: unindent does not match any outer indentation level
Hawkins
  • 700
  • 6
  • 21
  • Your code does not even run for me, how are you creating an instance? – Padraic Cunningham Sep 14 '14 at 22:42
  • To post your code properly here it needs to be indented by *another* 4 spaces; that's why `class Set:` isn't displayed in the code block. BTW, you can change `while` to `if` in your `remove_element` and `has_element` methods. – PM 2Ring Sep 14 '14 at 23:43

2 Answers2

2

You've mixed tabs and spaces. Don't do that; this is what happens when you do. Python thinks some of your methods are actually internal to some of your other methods, so the Set class doesn't actually have __str__ or __repr__ methods.

Fix your indentation, and your problem will go away. To avoid such problems in the future, turn on "show whitespace" in your editor, and try running Python with the -tt command line option if you think you might be seeing tab-related bugs.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • @quintillion: You probably haven't fixed your indentation properly. Go through it again, and you'll likely find something you missed. – user2357112 Sep 15 '14 at 02:42
1

There is another problem at:

if self.list.len == 0:

you probably meant to do:

if len(self.list) == 0:

Once this issue is fixed, the code works:

s = Set()
s.add_element(1)
s.add_element(1)
s.add_element(2)
s.add_element(3)
print s  # prints {1, 2, 3}
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Or maybe `if self.list.__len__() == 0` – Padraic Cunningham Sep 14 '14 at 22:43
  • @PadraicCunningham yes, that should work too (though it's ugly :) – Nir Alfasi Sep 14 '14 at 22:45
  • Yep, I think that is what probably confused the OP though. I would still love to know how the OP got the code to run – Padraic Cunningham Sep 14 '14 at 22:46
  • Well I'm using xcode and the indentation settings were pretty weird so I altered them. Evidently I tabs are bad, and I did not know that. Maybe xcode somehow operates around it if it was changed in the settings? Regardless, I replaced all tab characters with 4 spaces and updated the original code. It might work for you, now. – Hawkins Sep 14 '14 at 22:58
  • @PadraicCunningham assuming no indentation errors (after using pycharm auto-indent for instance), and fixing the problem I mentioned above, it seems the code works - though I have to admit that I didn't test each and every function. – Nir Alfasi Sep 14 '14 at 23:11
  • @alfasin did you happen to test the str or repr functions? I cannot get past the unindent errors at this point to figure out if they even work. – Hawkins Sep 14 '14 at 23:14
  • @quintillion yes - both functions are working for me - it's only a matter of indentation + the issue I mentioned in the answer above. – Nir Alfasi Sep 14 '14 at 23:20
  • @quintillion, it worked fine for me once I used `if self.list.__len__() `, what I meant was how did it work at all for you to get any output as your code was invalid syntactically. – Padraic Cunningham Sep 15 '14 at 00:06
  • @PadraicCunningham I have no idea. It just worked with xcode, I don't know why. I am not familiar with either xcode or python so I really don't know what to say, sorry! But you're right, it does seem rather strange that it even ran for me in the first place. – Hawkins Sep 15 '14 at 00:08