Question about types of objects

This little algorithm generates numbers, including duplicates, and appends them to a list. To remove the duplicates, I then use the “set” function on the list:

a2b =
for a in range(2, 11):
for b in range(2, 11):
a2b.append(a**b)
a2b = set(a2b)

This works fine and I can get the length of the list (len(a2b)). Seeking to learn something I wondered if I could declare the list as a set from the start and eliminate the last line:

a2b = set()
for a in range(2, 11):
for b in range(2, 11):
a2b.add(a**b)

And yes, that works too BUT, only after a lot of error messages when I finally tried changing “append” to “add.” Conversely, in the first case, “add” won’t work. So I did learn that:

  1. ‘list’ object has no attribute ‘add’
  2. ‘set’ object has no attribute ‘append’

but this is not totally enlightening. I would like to take away a little more from this experience. Does every possible object have its own set of attributes? Are there groups of objects that share sets of attributes?

Can someone throw some light on this, please? Thanks very much.

1 Like

Please start with some default literature to learn Python. Your path of learning by trial and error on this level will take a long time.

1 Like

Hi Igor,

  1. ‘list’ object has no attribute ‘add’
  2. ‘set’ object has no attribute ‘append’

This is correct. Lists and sets provide different methods because they
do different things.

Lists are ordered, so they have a method for appending to the end
of the list, list.append, and another method for inserting at the start
or middle of the list, list.insert.

Sets are unordered: they have no inherent order to their elements. Of
course when you print them, they must be printed in some order, but that
order is not guaranteed to be the same as the order you assembled the
set:

> s = {'a', 'b', 'c', 'd'}
> s
{'c', 'b', 'd', 'a'}

So sets do not have methods for inserting in the middle or appending to
the end, they only have a generic “add this element somewhere in the
set” method.

Does every possible object have its own set of attributes?

Yes.

Are there groups of objects that share sets of attributes?

Yes, but not as many as you might expect.

Apart from reading the documentation, there are two helpful tools that
you can use in the interactive interpreter.

(1) The dir() function takes an object and gives you a list of
attributes (mostly methods):

> dir('hello world')
['__add__', '__class__', [...]
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 
'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 
'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 
'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 
'upper', 'zfill']

(The list is sometimes quite long. I recommend that for now you ignore
all the attributes starting and ending with underscores. They are for
more advanced use.)

(2) The help() function opens a help screen in your terminal where you
can read documentation. Try:

help()

to get started, or something like:

help('hello world')

to find out about strings. Sometimes the help documentation can be quite
long; you can focus on a single method by giving it as an argument
without the parentheses/brackets, like this:

help(''.upper)  # Give help on the str.upper method only.

Note that if you accidentally do this:

help(''.upper())

you won’t get the expected help screen, so be careful about not adding
the unwanted brackets.

Good luck!

2 Likes

I have literature and refer to it frequently. Occasionally I still have a question. In the past I have received helpful replies on this site. I was hoping for the same this time.

1 Like

Just what I needed. Thanks for a useful and thoughtful response.
Ig.

1 Like

Usually one of the first things explained in literature is the dir function which has been previously mentioned.

1 Like