Is there a simple way of comparing the string representation of an object to each object in a list?
The example code below (Python 2.7) works as intended, but I assume there's a much nicer way of doing this in Python!
class url(object):
def __init__(self, address):
self.address = address
def __str__(self):
return self.address
list_of_urls = [url('http://www.example.foo'), url('http://www.example.bar')]
test_url = url('http://www.example.foobar')
test_url_listed = False
for link in list_of_urls:
if str(test_url) == str(link):
test_url_listed = True
if not test_url_listed:
list_of_urls.append(test_url)
Is it possible to make it closer in structure to the following?
if test_url not in list_of_urls:
list_of_urls.append(test_url)
(As is, this fails since it compares the objects and not the strings they represent.)