Python set add method

python collection set


class set(object):
    def add(self, *args, **kwargs):  
    
 
add*args

ss = {1,3,5}
ss.add(7,9)  
Mar.28,2021

aren't you and I looking at the same source code (lib\ sets.py)?

class Set(BaseSet):
    def add(self, element):
        """Add an element to a set.

        This has no effect if the element is already present.
        """
        try:
            self._data[element] = True
        except TypeError:
            transform = getattr(element, "__as_immutable__", None)
            if transform is None:
                raise -sharp re-raise the TypeError exception we caught
            self._data[transform()] = True

The implementation part of the

collection is implemented in C language. I don't know where the "source code" you are looking for. Some IDE will do some declaration work such as PyCharm in order to be friendly. The subject should just want to find out how the parameters in add are declared. You can see the property method of the set object in setobject.c :

static PyMethodDef set_methods[] = {
    {"add",             (PyCFunction)set_add,           METH_O, add_doc},
    {"clear",           (PyCFunction)set_clear,         METH_NOARGS, clear_doc},
    {"difference",      (PyCFunction)set_difference_multi,      METH_VARARGS, difference_doc},
    ...
}

set.add the implementation part is set_add , you can take a look at its declaration:

static PyObject *
set_add(PySetObject *so, PyObject *key)
{
    if (set_add_key(so, key))
        return NULL;
    Py_RETURN_NONE;
}

has only one parameter, that is, it can only be added by a single element.

In addition, you can see the description of python through its built-in helper function help () :

.
>>> help(set.add)
Help on method_descriptor:

add(...)
    Add an element to a set.

    This has no effect if the element is already present.

>>>

Source of the code: python/cpython/blob/master/Objects/setobject.c-sharpL2056" rel=" nofollow noreferrer "> python/cpython/blob/master/Objects/setobject.c-sharpL2056" rel= "nofollow noreferrer" > https://github.com/python/cpy.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3f3de-34843.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3f3de-34843.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?