Ask Your Question

Revision history [back]

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'?

click to hide/show revision 2
retagged

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'?

click to hide/show revision 3
retagged

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'?