Set in Class initialization parses string as set of chars

Python 3.10.1,

class ItemRow:
    def __Init__(self, name, addresses, phone_numbers):
        self.friendName = name
        self.addrs = set(addresses)
        self.phone_nos = set(phone_numbers)
friendObject = ItemRow("Peppermint Patty", "2301 Hardies Ln", "")


The output is
{‘e’, ‘H’, ‘3’, ‘n’, ‘1’, etc., etc.}. I.e., each character is added to the set instead of the entire string being a member of the set.

I can add to the set fine, i.e., if I add “2301 Hardies Ln” after instantiating the object, the addrs set is all of the original caracters, plus an element equal to the complete string.

I found a workaround, in the class init function, initialize the set as:

self.addrs = {addresses}

I’d still like to understand why set(addresses) interprets the string as a set of characters.

Hello, @lazyviking!

set() takes an iterable as an argument. Then, it adds that iterable’s items into a set.
-String objects are also iterable, you can loop over them( for i in string […] ) or get their specific characters with indexing.-

And, because set() loops over the argument, two code lines below are equvialent to each other:

set( ["H","E","L","L","O"] )

In your case, set() loops over your string and adds it’s characters into a set object.
Note: If you don’t want to use {addresses} , you can also try this:

set( [addresses] )#Here, you are supplying a list as an iterable instead of a string. 

Hope, this helps.
You can see the documentation page.


"I’d still like to understand why set(addresses) interprets the string as a set of characters.

Because you are literally asking Python to convert the string to a set of characters.

That’s what set(string) does. Just as list(string) converts the string to a list of characters. And tuple(string) into a tuple of characters.

Strings are iterable, and you iterate over their characters (or to be precise, their Unicode code-points, which mostly represent what we consider characters):

for char in "Hello World":

prints “H”, “e”, “l”, “l”, “o” etc, one per line.

When you call set(obj), or list(obj), etc, set (list etc) iterates over the elements of obj. So we have:

obj = (10, 20, 30)  # A tuple.

is like {10, 20, 30} (three elements), not the set {(10, 20, 30)} (one element, a tuple). To get the second one-element set, you write {obj}.

Strings follow the same logic.