ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 04 Jun 2019 18:51:32 -0500Adding elements to a sethttps://ask.sagemath.org/question/46797/adding-elements-to-a-set/I learned Python a long while ago before I decided to get into SageMath for mathematical purposes.
I am a bit puzzled that I don't seem to have a way to add an element to a set when using the SageMath definition 'Set()', but there seems to be no problem when doing this using the Python definition 'set()', namely:
a=2; b=3; c=4
D=Set([a,b])
D.add(c)
gives the error
'Set_object_enumerated_with_category' object has no attribute 'add'
whereas
a=2; b=3; c=4
D=set([a,b])
D.add(c)
gives no problem (notice the difference in capital letter when creating set).
I am using the following workaround
a=2; b=3; c=4
D=Set([a,b])
D=D.union(Set([c]))
but it seems rather artificial. Is there no more natural way to proceed? Is there a natural reason to explain why 'add' is not a method of 'Set'?Tue, 04 Jun 2019 13:58:51 -0500https://ask.sagemath.org/question/46797/adding-elements-to-a-set/Answer by rburing for <p>I learned Python a long while ago before I decided to get into SageMath for mathematical purposes.</p>
<p>I am a bit puzzled that I don't seem to have a way to add an element to a set when using the SageMath definition 'Set()', but there seems to be no problem when doing this using the Python definition 'set()', namely:</p>
<pre><code>a=2; b=3; c=4
D=Set([a,b])
D.add(c)
</code></pre>
<p>gives the error</p>
<pre><code>'Set_object_enumerated_with_category' object has no attribute 'add'
</code></pre>
<p>whereas</p>
<pre><code>a=2; b=3; c=4
D=set([a,b])
D.add(c)
</code></pre>
<p>gives no problem (notice the difference in capital letter when creating set).</p>
<p>I am using the following workaround </p>
<pre><code>a=2; b=3; c=4
D=Set([a,b])
D=D.union(Set([c]))
</code></pre>
<p>but it seems rather artificial. Is there no more natural way to proceed? Is there a natural reason to explain why 'add' is not a method of 'Set'?</p>
https://ask.sagemath.org/question/46797/adding-elements-to-a-set/?answer=46799#post-id-46799The [documentation on sets](http://doc.sagemath.org/html/en/reference/sets/sage/sets/set.html) explains it well:
> If `X` is a list, tuple, Python set, or `X.is_finite()` is `True`, this returns a wrapper around Python’s enumerated immutable `frozenset` type with extra functionality. Otherwise it returns a more formal wrapper.
> If you need the functionality of mutable sets, use Python’s builtin set type.
So `Set([a,b])` acts more like Python's `frozenset` (hence it has no method to add an element).
If you want a mutable set, just use Python's `set` type.Tue, 04 Jun 2019 14:13:51 -0500https://ask.sagemath.org/question/46797/adding-elements-to-a-set/?answer=46799#post-id-46799Comment by Jesus Martinez Garcia for <p>The <a href="http://doc.sagemath.org/html/en/reference/sets/sage/sets/set.html">documentation on sets</a> explains it well:</p>
<blockquote>
<p>If <code>X</code> is a list, tuple, Python set, or <code>X.is_finite()</code> is <code>True</code>, this returns a wrapper around Python’s enumerated immutable <code>frozenset</code> type with extra functionality. Otherwise it returns a more formal wrapper.</p>
<p>If you need the functionality of mutable sets, use Python’s builtin set type.</p>
</blockquote>
<p>So <code>Set([a,b])</code> acts more like Python's <code>frozenset</code> (hence it has no method to add an element).</p>
<p>If you want a mutable set, just use Python's <code>set</code> type.</p>
https://ask.sagemath.org/question/46797/adding-elements-to-a-set/?comment=46800#post-id-46800Thank you. Incidentally, I think that suggest an answer for my last question ('Is there a natural reason to explain why 'add' is not a method of 'Set'?'): if one wants a mathematical notion of set, we want to be able to create 'set of sets' naturally.
Now I remember having this problem in Python when dealing with sets of sets: because the set is mutable (and hence not hashable), one has to create a frozen set, and then a set of frozen sets. From that point of view the Sage design is more natural.Tue, 04 Jun 2019 18:51:32 -0500https://ask.sagemath.org/question/46797/adding-elements-to-a-set/?comment=46800#post-id-46800