Skip to content

# `` term

This module defines the different types of terms. Terms are the kinds of objects that can appear in a quoted/asserted triple. This includes those that are core to RDF:

- Blank Nodes - Blank Nodes
- URI References - URI References
- Literals - Literals (which consist of a literal value, datatype and language tag)

Those that extend the RDF model into N3:

- `QuotedGraph` - Formulae
- `Variable` - Universal Quantifications (Variables)

And those that are primarily for matching against ‘Nodes’ in the underlying Graph:

- REGEX Expressions
- Date Ranges
- Numerical Ranges

Classes:

- `BNode` –

  RDF 1.1’s Blank Nodes Section: https://www.w3.org/TR/rdf11-concepts/#section-blank-nodes

- `IdentifiedNode` –

  An abstract class, primarily defined to identify Nodes that are not Literals.

- `Identifier` –

  See http://www.w3.org/2002/07/rdf-identifer-terminology/

- `Literal` –

  RDF 1.1’s Literals Section: http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal

- `Node` –

  A Node in the Graph.

- `URIRef` –

  RDF 1.1’s IRI Section

- `Variable` –

  A Variable - this is used for querying, or in Formula aware

Functions:

- `bind` –

  register a new datatype<->pythontype binding

## `` XSDToPython `module-attribute`

    XSDToPython: Dict[Optional[str], Optional[Callable[[str], Any]]] = {None: None, URIRef(_XSD_PFX + 'time'): parse_time, URIRef(_XSD_PFX + 'date'): parse_xsd_date, URIRef(_XSD_PFX + 'dateTime'): parse_datetime, URIRef(_XSD_PFX + 'duration'): parse_xsd_duration, URIRef(_XSD_PFX + 'dayTimeDuration'): parse_xsd_duration, URIRef(_XSD_PFX + 'yearMonthDuration'): parse_xsd_duration, URIRef(_XSD_PFX + 'hexBinary'): _unhexlify, URIRef(_XSD_PFX + 'string'): None, URIRef(_XSD_PFX + 'normalizedString'): None, URIRef(_XSD_PFX + 'token'): None, URIRef(_XSD_PFX + 'language'): None, URIRef(_XSD_PFX + 'boolean'): _parseBoolean, URIRef(_XSD_PFX + 'decimal'): Decimal, URIRef(_XSD_PFX + 'integer'): long_type, URIRef(_XSD_PFX + 'nonPositiveInteger'): long_type, URIRef(_XSD_PFX + 'long'): long_type, URIRef(_XSD_PFX + 'nonNegativeInteger'): long_type, URIRef(_XSD_PFX + 'negativeInteger'): long_type, URIRef(_XSD_PFX + 'int'): int, URIRef(_XSD_PFX + 'unsignedLong'): long_type, URIRef(_XSD_PFX + 'positiveInteger'): long_type, URIRef(_XSD_PFX + 'short'): int, URIRef(_XSD_PFX + 'unsignedInt'): int, URIRef(_XSD_PFX + 'byte'): int, URIRef(_XSD_PFX + 'unsignedShort'): int, URIRef(_XSD_PFX + 'unsignedByte'): int, URIRef(_XSD_PFX + 'float'): float, URIRef(_XSD_PFX + 'double'): float, URIRef(_XSD_PFX + 'base64Binary'): b64decode, URIRef(_XSD_PFX + 'anyURI'): None, _RDF_XMLLITERAL: _parseXML}

## `` __all__ `module-attribute`

    __all__ = ['bind', '_is_valid_uri', 'Node', 'IdentifiedNode', 'Identifier', 'URIRef', 'BNode', 'Literal', 'Variable']

## `` logger `module-attribute`

    logger = getLogger(__name__)

## `` rdflib_skolem_genid `module-attribute`

    rdflib_skolem_genid = '/.well-known/genid/rdflib/'

## `` skolem_genid `module-attribute`

    skolem_genid = '/.well-known/genid/'

## `` skolems `module-attribute`

    skolems: Dict[str, BNode] = {}

## `` BNode

Bases: `IdentifiedNode`

RDF 1.1’s Blank Nodes Section: https://www.w3.org/TR/rdf11-concepts/#section-blank-nodes

Blank Nodes are local identifiers for unnamed nodes in RDF graphs that are used in some concrete RDF syntaxes or RDF store implementations. They are always locally scoped to the file or RDF store, and are not persistent or portable identifiers for blank nodes. The identifiers for Blank Nodes are not part of the RDF abstract syntax, but are entirely dependent on particular concrete syntax or implementation (such as Turtle, JSON-LD).

RDFLib’s `BNode` class makes unique IDs for all the Blank Nodes in a Graph but you should never expect, or reply on, BNodes’ IDs to match across graphs, or even for multiple copies of the same graph, if they are regenerated from some non-RDFLib source, such as loading from RDF data.

Methods:

- `__new__` –

  ### only store implementations should pass in a value

- `__reduce__` –
- `__repr__` –
- `n3` –
- `skolemize` –

  Create a URIRef “skolem” representation of the BNode, in accordance

Attributes:

- `__slots__` –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

### `` __new__

    __new__(value: Optional[str] = None, _sn_gen: Optional[Union[Callable[[], str], Generator]] = None, _prefix: str = _unique_id())

#### only store implementations should pass in a value

Source code in `rdflib/term.py`

    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493

    def __new__(
        cls,
        value: Optional[str] = None,
        _sn_gen: Optional[Union[Callable[[], str], Generator]] = None,
        _prefix: str = _unique_id(),
    ):
        """
        # only store implementations should pass in a value
        """
        if value is None:
            # so that BNode values do not collide with ones created with
            # a different instance of this module at some other time.
            if _sn_gen is not None:
                if callable(_sn_gen):
                    sn_result: Union[str, Generator] = _sn_gen()
                else:
                    sn_result = _sn_gen
                if isinstance(sn_result, GeneratorType):
                    node_id = next(sn_result)
                else:
                    node_id = sn_result
            else:
                node_id = uuid4().hex
            # note, for two (and only two) string variables,
            # concat with + is faster than f"{x}{y}"
            value = _prefix + f"{node_id}"
        else:
            # TODO: check that value falls within acceptable bnode value range
            # for RDF/XML needs to be something that can be serialized
            # as a nodeID for N3 ??  Unless we require these
            # constraints be enforced elsewhere?
            pass  # assert is_ncname(str(value)), "BNode identifiers
            # must be valid NCNames" _:[A-Za-z][A-Za-z0-9]*
            # http://www.w3.org/TR/2004/REC-rdf-testcases-20040210/#nodeID
        # type error: Incompatible return value type (got "Identifier", expected "BNode")
        return Identifier.__new__(cls, value)

### `` __reduce__

    __reduce__() -> Tuple[Type[BNode], Tuple[str]]

Source code in `rdflib/term.py`

    499
    500

    def __reduce__(self) -> Tuple[Type[BNode], Tuple[str]]:
        return (BNode, (str(self),))

### `` __repr__

    __repr__() -> str

Source code in `rdflib/term.py`

    502
    503
    504
    505
    506
    507

    def __repr__(self) -> str:
        if self.__class__ is BNode:
            clsName = "rdflib.term.BNode"  # noqa: N806
        else:
            clsName = self.__class__.__name__  # noqa: N806
        return f"{clsName}({str.__repr__(self)})"

### `` n3

    n3(namespace_manager: Optional[NamespaceManager] = None) -> str

Source code in `rdflib/term.py`

    495
    496
    497

    def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
        # note - for two strings, concat with + is faster than f"{x}{y}"
        return "_:" + self

### `` skolemize

    skolemize(authority: Optional[str] = None, basepath: Optional[str] = None) -> URIRef

Create a URIRef “skolem” representation of the BNode, in accordance with http://www.w3.org/TR/rdf11-concepts/#section-skolemization

Added in version 4.0

Source code in `rdflib/term.py`

    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522

    def skolemize(
        self, authority: Optional[str] = None, basepath: Optional[str] = None
    ) -> URIRef:
        """Create a URIRef "skolem" representation of the BNode, in accordance
        with http://www.w3.org/TR/rdf11-concepts/#section-skolemization

        Added in version 4.0
        """
        if authority is None:
            authority = _SKOLEM_DEFAULT_AUTHORITY
        if basepath is None:
            basepath = rdflib_skolem_genid
        skolem = basepath + str(self)
        return URIRef(urljoin(authority, skolem))

## `` Genid

Bases: `URIRef`

Attributes:

- `__slots__` –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

## `` IdentifiedNode

Bases: `Identifier`

An abstract class, primarily defined to identify Nodes that are not Literals.

The name “Identified Node” is not explicitly defined in the RDF specification, but can be drawn from this section: https://www.w3.org/TR/rdf-concepts/#section-URI-Vocabulary

Methods:

- `__getnewargs__` –
- `n3` –
- `toPython` –

Attributes:

- `__slots__` –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

### `` __getnewargs__

    __getnewargs__() -> Tuple[str]

Source code in `rdflib/term.py`

    256
    257

    def __getnewargs__(self) -> Tuple[str]:
        return (str(self),)

### `` n3

    n3(namespace_manager: Optional[NamespaceManager] = None) -> str

Source code in `rdflib/term.py`

    259
    260

    def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
        raise NotImplementedError()

### `` toPython

    toPython() -> str

Source code in `rdflib/term.py`

    262
    263

    def toPython(self) -> str:  # noqa: N802
        return str(self)

## `` Identifier

Bases: `Node`, `str`

See http://www.w3.org/2002/07/rdf-identifer-terminology/ regarding choice of terminology.

Methods:

- `__eq__` –

  Equality for Nodes.

- `__ge__` –
- `__gt__` –

  This implements ordering for Nodes.

- `__le__` –
- `__lt__` –
- `__ne__` –
- `__new__` –
- `eq` –

  A “semantic”/interpreted equality function,

- `neq` –

  A “semantic”/interpreted not equal function,

- `startswith` –

Attributes:

- `__hash__` –
- `__slots__` –

### `` __hash__ `class-attribute` `instance-attribute`

    __hash__ = __hash__

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

### `` __eq__

    __eq__(other: Any) -> bool

Equality for Nodes.

    >>> BNode("foo")==None
    False
    >>> BNode("foo")==URIRef("foo")
    False
    >>> URIRef("foo")==BNode("foo")
    False
    >>> BNode("foo")!=URIRef("foo")
    True
    >>> URIRef("foo")!=BNode("foo")
    True
    >>> Variable('a')!=URIRef('a')
    True
    >>> Variable('a')!=Variable('a')
    False

Source code in `rdflib/term.py`

    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192

    def __eq__(self, other: Any) -> bool:
        """Equality for Nodes.

        ```python
        >>> BNode("foo")==None
        False
        >>> BNode("foo")==URIRef("foo")
        False
        >>> URIRef("foo")==BNode("foo")
        False
        >>> BNode("foo")!=URIRef("foo")
        True
        >>> URIRef("foo")!=BNode("foo")
        True
        >>> Variable('a')!=URIRef('a')
        True
        >>> Variable('a')!=Variable('a')
        False

        ```
        """

        if type(self) is type(other):
            return str(self) == str(other)
        else:
            return False

### `` __ge__

    __ge__(other: Any) -> bool

Source code in `rdflib/term.py`

    228
    229
    230
    231
    232

    def __ge__(self, other: Any) -> bool:
        r = self.__gt__(other)
        if r:
            return True
        return self == other

### `` __gt__

    __gt__(other: Any) -> bool

This implements ordering for Nodes.

This tries to implement this: http://www.w3.org/TR/sparql11-query/#modOrderBy

Variables are not included in the SPARQL list, but they are greater than BNodes and smaller than everything else

Source code in `rdflib/term.py`

    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210

    def __gt__(self, other: Any) -> bool:
        """This implements ordering for Nodes.

        This tries to implement this:
        http://www.w3.org/TR/sparql11-query/#modOrderBy

        Variables are not included in the SPARQL list, but
        they are greater than BNodes and smaller than everything else
        """
        if other is None:
            return True  # everything bigger than None
        elif type(self) is type(other):
            return str(self) > str(other)
        elif isinstance(other, Node):
            return _ORDERING[type(self)] > _ORDERING[type(other)]

        return NotImplemented

### `` __le__

    __le__(other: Any) -> bool

Source code in `rdflib/term.py`

    222
    223
    224
    225
    226

    def __le__(self, other: Any) -> bool:
        r = self.__lt__(other)
        if r:
            return True
        return self == other

### `` __lt__

    __lt__(other: Any) -> bool

Source code in `rdflib/term.py`

    212
    213
    214
    215
    216
    217
    218
    219
    220

    def __lt__(self, other: Any) -> bool:
        if other is None:
            return False  # Nothing is less than None
        elif type(self) is type(other):
            return str(self) < str(other)
        elif isinstance(other, Node):
            return _ORDERING[type(self)] < _ORDERING[type(other)]

        return NotImplemented

### `` __ne__

    __ne__(other: Any) -> bool

Source code in `rdflib/term.py`

    164
    165

    def __ne__(self, other: Any) -> bool:
        return not self.__eq__(other)

### `` __new__

    __new__(value: str)

Source code in `rdflib/term.py`

    151
    152

    def __new__(cls, value: str):
        return str.__new__(cls, value)

### `` eq

    eq(other: Any) -> bool

A “semantic”/interpreted equality function, by default, same as eq

Source code in `rdflib/term.py`

    154
    155
    156
    157

    def eq(self, other: Any) -> bool:
        """A "semantic"/interpreted equality function,
        by default, same as __eq__"""
        return self.__eq__(other)

### `` neq

    neq(other: Any) -> bool

A “semantic”/interpreted not equal function, by default, same as ne

Source code in `rdflib/term.py`

    159
    160
    161
    162

    def neq(self, other: Any) -> bool:
        """A "semantic"/interpreted not equal function,
        by default, same as __ne__"""
        return self.__ne__(other)

### `` startswith

    startswith(prefix: str, start=..., end=...) -> bool

Source code in `rdflib/term.py`

    237
    238

    def startswith(self, prefix: str, start=..., end=...) -> bool:  # type: ignore[override] # FIXME
        return str(self).startswith(str(prefix))

## `` Literal

Bases: `Identifier`

RDF 1.1’s Literals Section: http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal

Literals are used for values such as strings, numbers, and dates.

A literal in an RDF graph consists of two or three elements:

- a lexical form, being a Unicode string, which SHOULD be in Normal Form C
- a datatype IRI, being an IRI identifying a datatype that determines how the lexical form maps to a literal value, and
- if and only if the datatype IRI is `http://www.w3.org/1999/02/22-rdf-syntax-ns#langString`, a non-empty language tag. The language tag MUST be well-formed according to section 2.2.9 of `Tags for identifying languages <http://tools.ietf.org/html/bcp47>`_.

A literal is a language-tagged string if the third element is present. Lexical representations of language tags MAY be converted to lower case. The value space of language tags is always in lower case.

For valid XSD datatypes, the lexical form is optionally normalized at construction time. Default behaviour is set by `rdflib.NORMALIZE_LITERALS` and can be overridden by the normalize parameter to `__new__`

Equality and hashing of Literals are done based on the lexical form, i.e.:

    >>> from rdflib.namespace import XSD
    >>> Literal('01') != Literal('1')  # clear - strings differ
    True

but with data-type they get normalized:

    >>> Literal('01', datatype=XSD.integer) != Literal('1', datatype=XSD.integer)
    False

unless disabled:

    >>> Literal('01', datatype=XSD.integer, normalize=False) != Literal('1', datatype=XSD.integer)
    True

Value based comparison is possible:

    >>> Literal('01', datatype=XSD.integer).eq(Literal('1', datatype=XSD.float))
    True

The eq method also provides limited support for basic python types:

    >>> Literal(1).eq(1) # fine - int compatible with xsd:integer
    True
    >>> Literal('a').eq('b') # fine - str compatible with plain-lit
    False
    >>> Literal('a', datatype=XSD.string).eq('a') # fine - str compatible with xsd:string
    True
    >>> Literal('a').eq(1) # not fine, int incompatible with plain-lit
    NotImplemented

Greater-than/less-than ordering comparisons are also done in value space, when compatible datatypes are used. Incompatible datatypes are ordered by DT, or by lang-tag. For other nodes the ordering is None < BNode < URIRef < Literal

Any comparison with non-rdflib Node are “NotImplemented” In PY3 this is an error.

    >>> from rdflib import Literal, XSD
    >>> lit2006 = Literal('2006-01-01',datatype=XSD.date)
    >>> lit2006.toPython()
    datetime.date(2006, 1, 1)
    >>> lit2006 < Literal('2007-01-01',datatype=XSD.date)
    True
    >>> Literal(datetime.utcnow()).datatype
    rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime')
    >>> Literal(1) > Literal(2) # by value
    False
    >>> Literal(1) > Literal(2.0) # by value
    False
    >>> Literal('1') > Literal(1) # by DT
    True
    >>> Literal('1') < Literal('1') # by lexical form
    False
    >>> Literal('a', lang='en') > Literal('a', lang='fr') # by lang-tag
    False
    >>> Literal(1) > URIRef('foo') # by node-type
    True

The > < operators will eat this NotImplemented and throw a TypeError (py3k):

    >>> Literal(1).__gt__(2.0)
    NotImplemented

Methods:

- `__abs__` –

  Implements absolute value operation for Literals with numeric values.

- `__add__` –

  ```python

- `__bool__` –

  Determines the truth value of the Literal.

- `__eq__` –

  Equality operator for Literals.

- `__ge__` –
- `__getstate__` –
- `__gt__` –

  Implements the greater-than comparison for Literals.

- `__hash__` –

  Hash function for Literals to enable their use as dictionary keys.

- `__invert__` –

  Implements bitwise NOT operation for Literals with numeric values.

- `__le__` –

  Less than or equal operator for Literals.

- `__lt__` –
- `__neg__` –

  Implements unary negation for Literals with numeric values.

- `__new__` –

  Create a new Literal instance.

- `__pos__` –

  Implements unary plus operation for Literals with numeric values.

- `__reduce__` –
- `__repr__` –
- `__setstate__` –
- `__sub__` –

  Implements subtraction between Literals or between a Literal and a Python object.

- `eq` –

  Compare the value of this literal with something else.

- `n3` –

  Returns a representation in the N3 format.

- `neq` –
- `normalize` –

  Returns a new literal with a normalised lexical representation

- `toPython` –

  Returns an appropriate python datatype derived from this RDF Literal

Attributes:

- `__slots__` –
- `datatype` (`Optional[URIRef]`) –
- `ill_typed` (`Optional[bool]`) –

  For `recognized datatype IRIs

- `language` (`Optional[str]`) –
- `value` (`Any`) –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ('_language', '_datatype', '_value', '_ill_typed')

### `` datatype `property`

    datatype: Optional[URIRef]

### `` ill_typed `property`

    ill_typed: Optional[bool]

For `recognized datatype IRIs <https://www.w3.org/TR/rdf11-concepts/#dfn-recognized-datatype-iris>`, this value will be `True` if the literal is ill formed, otherwise it will be `False`. `Literal.value` (i.e. the `literal value <https://www.w3.org/TR/rdf11-concepts/#dfn-literal-value>`) should always be defined if this property is `False`, but should not be considered reliable if this property is `True`.

If the literal’s datatype is `None` or not in the set of `recognized datatype IRIs <https://www.w3.org/TR/rdf11-concepts/#dfn-recognized-datatype-iris>`_ this value will be `None`.

### `` language `property`

    language: Optional[str]

### `` value `property`

    value: Any

### `` __abs__

    __abs__() -> Literal

Implements absolute value operation for Literals with numeric values.

Example

    # Absolute value of a negative integer Literal
    abs(Literal(-1))
    # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # Using a string with a datatype
    from rdflib.namespace import XSD
    abs(Literal("-1", datatype=XSD.integer))
    # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # This will raise a TypeError
    abs(Literal("1"))
    # TypeError: Not a number; rdflib.term.Literal('1')

Source code in `rdflib/term.py`

    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104

    def __abs__(self) -> Literal:
        """Implements absolute value operation for Literals with numeric values.

        Example:
            ```python
            # Absolute value of a negative integer Literal
            abs(Literal(-1))
            # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # Using a string with a datatype
            from rdflib.namespace import XSD
            abs(Literal("-1", datatype=XSD.integer))
            # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # This will raise a TypeError
            abs(Literal("1"))
            # TypeError: Not a number; rdflib.term.Literal('1')
            ```
        """
        if isinstance(self.value, (int, long_type, float)):
            return Literal(self.value.__abs__())
        else:
            raise TypeError(f"Not a number; {self!r}")

### `` __add__

    __add__(val: Any) -> Literal

    >>> from rdflib.namespace import XSD
    >>> Literal(1) + 1
    rdflib.term.Literal('2', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
    >>> Literal("1") + "1"
    rdflib.term.Literal('11')

    # Handling dateTime/date/time based operations in Literals
    >>> a = Literal('2006-01-01T20:50:00', datatype=XSD.dateTime)
    >>> b = Literal('P31D', datatype=XSD.duration)
    >>> (a + b)
    rdflib.term.Literal('2006-02-01T20:50:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))
    >>> from rdflib.namespace import XSD
    >>> a = Literal('2006-07-01T20:52:00', datatype=XSD.dateTime)
    >>> b = Literal('P122DT15H58M', datatype=XSD.duration)
    >>> (a + b)
    rdflib.term.Literal('2006-11-01T12:50:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))

Source code in `rdflib/term.py`

    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901

    def __add__(self, val: Any) -> Literal:
        """
        ```python
        >>> from rdflib.namespace import XSD
        >>> Literal(1) + 1
        rdflib.term.Literal('2', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
        >>> Literal("1") + "1"
        rdflib.term.Literal('11')

        # Handling dateTime/date/time based operations in Literals
        >>> a = Literal('2006-01-01T20:50:00', datatype=XSD.dateTime)
        >>> b = Literal('P31D', datatype=XSD.duration)
        >>> (a + b)
        rdflib.term.Literal('2006-02-01T20:50:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))
        >>> from rdflib.namespace import XSD
        >>> a = Literal('2006-07-01T20:52:00', datatype=XSD.dateTime)
        >>> b = Literal('P122DT15H58M', datatype=XSD.duration)
        >>> (a + b)
        rdflib.term.Literal('2006-11-01T12:50:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))

        ```
        """

        # if no val is supplied, return this Literal
        if val is None:
            return self

        # convert the val to a Literal, if it isn't already one
        if not isinstance(val, Literal):
            val = Literal(val)

        # if self is datetime based and value is duration
        if (
            self.datatype in (_XSD_DATETIME, _XSD_DATE)
            and val.datatype in _TIME_DELTA_TYPES
        ):
            date1: Union[datetime, date] = self.toPython()
            duration: Union[Duration, timedelta] = val.toPython()
            difference = date1 + duration
            return Literal(difference, datatype=self.datatype)

        # if self is time based and value is duration
        elif self.datatype == _XSD_TIME and val.datatype in _TIME_DELTA_TYPES:
            selfv: time = self.toPython()
            valv: Union[Duration, timedelta] = val.toPython()
            sdt = datetime.combine(date(2000, 1, 1), selfv) + valv
            return Literal(sdt.time(), datatype=self.datatype)

        # if self is datetime based and value is not or vice versa
        elif (
            (
                self.datatype in _ALL_DATE_AND_TIME_TYPES
                and val.datatype not in _ALL_DATE_AND_TIME_TYPES
            )
            or (
                self.datatype not in _ALL_DATE_AND_TIME_TYPES
                and val.datatype in _ALL_DATE_AND_TIME_TYPES
            )
            or (
                self.datatype in _TIME_DELTA_TYPES
                and (
                    (val.datatype not in _TIME_DELTA_TYPES)
                    or (self.datatype != val.datatype)
                )
            )
        ):
            raise TypeError(
                f"Cannot add a Literal of datatype {str(val.datatype)} to a Literal of datatype {str(self.datatype)}"
            )

        # if the datatypes are the same, just add the Python values and convert back
        if self.datatype == val.datatype:
            return Literal(
                self.toPython() + val.toPython(), self.language, datatype=self.datatype
            )
        # if the datatypes are not the same but are both numeric, add the Python values and strip off decimal junk
        # (i.e. tiny numbers (more than 17 decimal places) and trailing zeros) and return as a decimal
        elif (
            self.datatype in _NUMERIC_LITERAL_TYPES
            and val.datatype in _NUMERIC_LITERAL_TYPES
        ):
            return Literal(
                Decimal(
                    f"{round(Decimal(self.toPython()) + Decimal(val.toPython()), 15):f}".rstrip(
                        "0"
                    ).rstrip(
                        "."
                    )
                ),
                datatype=_XSD_DECIMAL,
            )
        # in all other cases, perform string concatenation
        else:
            try:
                s = str.__add__(self, val)
            except TypeError:
                s = str(self.value) + str(val)

            # if the original datatype is string-like, use that
            if self.datatype in _STRING_LITERAL_TYPES:
                new_datatype = self.datatype
            # if not, use string
            else:
                new_datatype = _XSD_STRING

            return Literal(s, self.language, datatype=new_datatype)

### `` __bool__

    __bool__() -> bool

Determines the truth value of the Literal.

Used for if statements, bool(literal), etc.

Source code in `rdflib/term.py`

    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023

    def __bool__(self) -> bool:
        """Determines the truth value of the Literal.

        Used for if statements, bool(literal), etc.
        """
        if self.value is not None:
            return bool(self.value)
        return len(self) != 0

### `` __eq__

    __eq__(other: Any) -> bool

Equality operator for Literals.

Literals are only equal to other literals.

Notes

“Two literals are equal if and only if all of the following hold: * The strings of the two lexical forms compare equal, character by character. * Either both or neither have language tags. * The language tags, if any, compare equal. * Either both or neither have datatype URIs. * The two datatype URIs, if any, compare equal, character by character.” – 6.5.1 Literal Equality (RDF: Concepts and Abstract Syntax)

Example

    Literal("1", datatype=URIRef("foo")) == Literal("1", datatype=URIRef("foo"))
    # True
    Literal("1", datatype=URIRef("foo")) == Literal("1", datatype=URIRef("foo2"))
    # False

    Literal("1", datatype=URIRef("foo")) == Literal("2", datatype=URIRef("foo"))
    # False
    Literal("1", datatype=URIRef("foo")) == "asdf"
    # False

    from rdflib import XSD
    Literal('2007-01-01', datatype=XSD.date) == Literal('2007-01-01', datatype=XSD.date)
    # True
    Literal('2007-01-01', datatype=XSD.date) == date(2007, 1, 1)
    # False

    Literal("one", lang="en") == Literal("one", lang="en")
    # True
    Literal("hast", lang='en') == Literal("hast", lang='de')
    # False

    Literal("1", datatype=XSD.integer) == Literal(1)
    # True
    Literal("1", datatype=XSD.integer) == Literal("01", datatype=XSD.integer)
    # True

Source code in `rdflib/term.py`

    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408

    def __eq__(self, other: Any) -> bool:
        """Equality operator for Literals.

        Literals are only equal to other literals.

        Notes:
            "Two literals are equal if and only if all of the following hold:
            * The strings of the two lexical forms compare equal, character by character.
            * Either both or neither have language tags.
            * The language tags, if any, compare equal.
            * Either both or neither have datatype URIs.
            * The two datatype URIs, if any, compare equal, character by character."
            -- 6.5.1 Literal Equality (RDF: Concepts and Abstract Syntax)

        Example:
            ```python
            Literal("1", datatype=URIRef("foo")) == Literal("1", datatype=URIRef("foo"))
            # True
            Literal("1", datatype=URIRef("foo")) == Literal("1", datatype=URIRef("foo2"))
            # False

            Literal("1", datatype=URIRef("foo")) == Literal("2", datatype=URIRef("foo"))
            # False
            Literal("1", datatype=URIRef("foo")) == "asdf"
            # False

            from rdflib import XSD
            Literal('2007-01-01', datatype=XSD.date) == Literal('2007-01-01', datatype=XSD.date)
            # True
            Literal('2007-01-01', datatype=XSD.date) == date(2007, 1, 1)
            # False

            Literal("one", lang="en") == Literal("one", lang="en")
            # True
            Literal("hast", lang='en') == Literal("hast", lang='de')
            # False

            Literal("1", datatype=XSD.integer) == Literal(1)
            # True
            Literal("1", datatype=XSD.integer) == Literal("01", datatype=XSD.integer)
            # True
            ```
        """
        if self is other:
            return True
        if other is None:
            return False
        # Directly accessing the member is faster than the property.
        if isinstance(other, Literal):
            return (
                self._datatype == other._datatype
                and (self._language.lower() if self._language else None)
                == (other._language.lower() if other._language else None)
                and str.__eq__(self, other)
            )

        return False

### `` __ge__

    __ge__(other: Any) -> bool

Source code in `rdflib/term.py`

    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281

    def __ge__(self, other: Any) -> bool:
        r = self.__gt__(other)
        if r:
            return True
        try:
            return self.eq(other)
        except TypeError:
            return NotImplemented

### `` __getstate__

    __getstate__() -> Tuple[None, Dict[str, Union[str, None]]]

Source code in `rdflib/term.py`

    788
    789

    def __getstate__(self) -> Tuple[None, Dict[str, Union[str, None]]]:
        return (None, dict(language=self.language, datatype=self.datatype))

### `` __gt__

    __gt__(other: Any) -> bool

Implements the greater-than comparison for Literals.

This is the base method for ordering comparisons - other comparison methods delegate here.

Implements the ordering rules described in http://www.w3.org/TR/sparql11-query/#modOrderBy

In summary: 1. Literals with compatible data-types are ordered in value space 2. Incompatible datatypes are ordered by their datatype URIs 3. Literals with language tags are ordered by their language tags 4. Plain literals come before xsd:string literals 5. In the node order: None < BNode < URIRef < Literal

Example

    from rdflib import XSD
    from decimal import Decimal

    # Comparing numeric literals in value space
    Literal(1) > Literal(2)  # int/int
    # False

    Literal(2.0) > Literal(1)  # double/int
    # True

    Literal(Decimal("3.3")) > Literal(2.0)  # decimal/double
    # True

    Literal(Decimal("3.3")) < Literal(4.0)  # decimal/double
    # True

    # Comparing string literals
    Literal('b') > Literal('a')  # plain lit/plain lit
    # True

    Literal('b') > Literal('a', datatype=XSD.string)  # plain lit/xsd:str
    # True

    # Incompatible datatypes ordered by DT
    Literal(1) > Literal("2")  # int>string
    # False

    # Langtagged literals ordered by lang tag
    Literal("a", lang="en") > Literal("a", lang="fr")
    # False

Source code in `rdflib/term.py`

    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241

    def __gt__(self, other: Any) -> bool:
        """Implements the greater-than comparison for Literals.

        This is the base method for ordering comparisons - other comparison methods delegate here.

        Implements the ordering rules described in http://www.w3.org/TR/sparql11-query/#modOrderBy

        In summary:
        1. Literals with compatible data-types are ordered in value space
        2. Incompatible datatypes are ordered by their datatype URIs
        3. Literals with language tags are ordered by their language tags
        4. Plain literals come before xsd:string literals
        5. In the node order: None < BNode < URIRef < Literal

        Example:
            ```python
            from rdflib import XSD
            from decimal import Decimal

            # Comparing numeric literals in value space
            Literal(1) > Literal(2)  # int/int
            # False

            Literal(2.0) > Literal(1)  # double/int
            # True

            Literal(Decimal("3.3")) > Literal(2.0)  # decimal/double
            # True

            Literal(Decimal("3.3")) < Literal(4.0)  # decimal/double
            # True

            # Comparing string literals
            Literal('b') > Literal('a')  # plain lit/plain lit
            # True

            Literal('b') > Literal('a', datatype=XSD.string)  # plain lit/xsd:str
            # True

            # Incompatible datatypes ordered by DT
            Literal(1) > Literal("2")  # int>string
            # False

            # Langtagged literals ordered by lang tag
            Literal("a", lang="en") > Literal("a", lang="fr")
            # False
            ```
        """
        if other is None:
            return True  # Everything is greater than None
        if isinstance(other, Literal):
            # Fast path for comparing numeric literals
            # that are not ill-typed and don't have a None value
            if (
                (
                    self.datatype in _NUMERIC_LITERAL_TYPES
                    and other.datatype in _NUMERIC_LITERAL_TYPES
                )
                and ((not self.ill_typed) and (not other.ill_typed))
                and (self.value is not None and other.value is not None)
            ):
                return self.value > other.value

            # plain-literals and xsd:string literals
            # are "the same"
            dtself = rdflib.util._coalesce(self.datatype, default=_XSD_STRING)
            dtother = rdflib.util._coalesce(other.datatype, default=_XSD_STRING)

            if dtself != dtother:
                if rdflib.DAWG_LITERAL_COLLATION:
                    return NotImplemented
                else:
                    return dtself > dtother

            if self.language != other.language:
                if not self.language:
                    return False
                elif not other.language:
                    return True
                else:
                    return self.language > other.language

            if self.value is not None and other.value is not None:
                if type(self.value) in _TOTAL_ORDER_CASTERS:
                    caster = _TOTAL_ORDER_CASTERS[type(self.value)]
                    return caster(self.value) > caster(other.value)

                try:
                    return self.value > other.value
                except TypeError:
                    pass

            if str(self) != str(other):
                return str(self) > str(other)

            # same language, same lexical form, check real dt
            # plain-literals come before xsd:string!
            if self.datatype != other.datatype:
                if self.datatype is None:
                    return False
                elif other.datatype is None:
                    return True
                else:
                    return self.datatype > other.datatype

            return False  # they are the same

        elif isinstance(other, Node):
            return True  # Literal are the greatest!
        else:
            return NotImplemented  # we can only compare to nodes

### `` __hash__

    __hash__() -> int

Hash function for Literals to enable their use as dictionary keys.

Example

    from rdflib.namespace import XSD
    a = {Literal('1', datatype=XSD.integer):'one'}
    Literal('1', datatype=XSD.double) in a
    # False

Notes

“Called for the key object for dictionary operations, and by the built-in function hash(). Should return a 32-bit integer usable as a hash value for dictionary operations. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g., using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.” – 3.4.1 Basic customization (Python)

“Two literals are equal if and only if all of the following hold: * The strings of the two lexical forms compare equal, character by character. * Either both or neither have language tags. * The language tags, if any, compare equal. * Either both or neither have datatype URIs. * The two datatype URIs, if any, compare equal, character by character.” – 6.5.1 Literal Equality (RDF: Concepts and Abstract Syntax)

Source code in `rdflib/term.py`

    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350

    def __hash__(self) -> int:  # type: ignore[override]
        """Hash function for Literals to enable their use as dictionary keys.

        Example:
            ```python
            from rdflib.namespace import XSD
            a = {Literal('1', datatype=XSD.integer):'one'}
            Literal('1', datatype=XSD.double) in a
            # False
            ```

        Notes:
            "Called for the key object for dictionary operations,
            and by the built-in function hash(). Should return
            a 32-bit integer usable as a hash value for
            dictionary operations. The only required property
            is that objects which compare equal have the same
            hash value; it is advised to somehow mix together
            (e.g., using exclusive or) the hash values for the
            components of the object that also play a part in
            comparison of objects." -- 3.4.1 Basic customization (Python)

            "Two literals are equal if and only if all of the following hold:
            * The strings of the two lexical forms compare equal, character by character.
            * Either both or neither have language tags.
            * The language tags, if any, compare equal.
            * Either both or neither have datatype URIs.
            * The two datatype URIs, if any, compare equal, character by character."
            -- 6.5.1 Literal Equality (RDF: Concepts and Abstract Syntax)
        """
        # don't use super()... for efficiency reasons, see Identifier.__hash__
        res = str.__hash__(self)
        # Directly accessing the member is faster than the property.
        if self._language:
            res ^= hash(self._language.lower())
        if self._datatype is not None:
            res ^= hash(self._datatype)
        return res

### `` __invert__

    __invert__() -> Literal

Implements bitwise NOT operation for Literals with numeric values.

Example

    # Bitwise NOT of a negative integer Literal
    ~(Literal(-1))
    # rdflib.term.Literal('0', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # Using a string with a datatype
    from rdflib.namespace import XSD
    ~(Literal("-1", datatype=XSD.integer))
    # rdflib.term.Literal('0', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # This will raise a TypeError
    ~(Literal("1"))
    # TypeError: Not a number; rdflib.term.Literal('1')

Source code in `rdflib/term.py`

    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129

    def __invert__(self) -> Literal:
        """Implements bitwise NOT operation for Literals with numeric values.

        Example:
            ```python
            # Bitwise NOT of a negative integer Literal
            ~(Literal(-1))
            # rdflib.term.Literal('0', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # Using a string with a datatype
            from rdflib.namespace import XSD
            ~(Literal("-1", datatype=XSD.integer))
            # rdflib.term.Literal('0', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # This will raise a TypeError
            ~(Literal("1"))
            # TypeError: Not a number; rdflib.term.Literal('1')
            ```
        """
        if isinstance(self.value, (int, long_type, float)):
            # type error: Unsupported operand type for ~ ("float")
            return Literal(self.value.__invert__())  # type: ignore[operator] # FIXME
        else:
            raise TypeError(f"Not a number; {self!r}")

### `` __le__

    __le__(other: Any) -> bool

Less than or equal operator for Literals.

Example

    from rdflib.namespace import XSD
    Literal('2007-01-01T10:00:00', datatype=XSD.dateTime) <= Literal('2007-01-01T10:00:00', datatype=XSD.dateTime)
    # True

Source code in `rdflib/term.py`

    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272

    def __le__(self, other: Any) -> bool:
        """Less than or equal operator for Literals.

        Example:
            ```python
            from rdflib.namespace import XSD
            Literal('2007-01-01T10:00:00', datatype=XSD.dateTime) <= Literal('2007-01-01T10:00:00', datatype=XSD.dateTime)
            # True
            ```
        """
        r = self.__lt__(other)
        if r:
            return True
        try:
            return self.eq(other)
        except TypeError:
            return NotImplemented

### `` __lt__

    __lt__(other: Any) -> bool

Source code in `rdflib/term.py`

    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254

    def __lt__(self, other: Any) -> bool:
        if other is None:
            return False  # Nothing is less than None
        if isinstance(other, Literal):
            try:
                return not self.__gt__(other) and not self.eq(other)
            except TypeError:
                return NotImplemented
        if isinstance(other, Node):
            return False  # all nodes are less-than Literals

        return NotImplemented

### `` __neg__

    __neg__() -> Literal

Implements unary negation for Literals with numeric values.

Example

    # Negating an integer Literal
    -Literal(1)
    # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # Negating a float Literal
    -Literal(10.5)
    # rdflib.term.Literal('-10.5', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'))

    # Using a string with a datatype
    from rdflib.namespace import XSD
    -Literal("1", datatype=XSD.integer)
    # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # This will raise a TypeError
    -Literal("1")
    # TypeError: Not a number; rdflib.term.Literal('1')

Source code in `rdflib/term.py`

    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052

    def __neg__(self) -> Literal:
        """Implements unary negation for Literals with numeric values.

        Example:
            ```python
            # Negating an integer Literal
            -Literal(1)
            # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # Negating a float Literal
            -Literal(10.5)
            # rdflib.term.Literal('-10.5', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'))

            # Using a string with a datatype
            from rdflib.namespace import XSD
            -Literal("1", datatype=XSD.integer)
            # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # This will raise a TypeError
            -Literal("1")
            # TypeError: Not a number; rdflib.term.Literal('1')
            ```
        """

        if isinstance(self.value, (int, long_type, float)):
            return Literal(self.value.__neg__())
        else:
            raise TypeError(f"Not a number; {self!r}")

### `` __new__

    __new__(lexical_or_value: Any, lang: Optional[str] = None, datatype: Optional[str] = None, normalize: Optional[bool] = None)

Create a new Literal instance.

Source code in `rdflib/term.py`

    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727

    def __new__(
        cls,
        lexical_or_value: Any,
        lang: Optional[str] = None,
        datatype: Optional[str] = None,
        normalize: Optional[bool] = None,
    ):
        """Create a new Literal instance."""
        if lang == "":
            lang = None  # no empty lang-tags in RDF

        normalize = normalize if normalize is not None else rdflib.NORMALIZE_LITERALS

        if lang is not None and datatype is not None:
            raise TypeError(
                "A Literal can only have one of lang or datatype, "
                "per http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal"
            )

        if lang is not None and not _is_valid_langtag(lang):
            raise ValueError(f"'{str(lang)}' is not a valid language tag!")

        if datatype is not None:
            datatype = URIRef(datatype)

        value = None
        ill_typed: Optional[bool] = None
        if isinstance(lexical_or_value, Literal):
            # create from another Literal instance

            lang = lang or lexical_or_value.language
            if datatype is not None:
                # override datatype
                value = _castLexicalToPython(lexical_or_value, datatype)
            else:
                datatype = lexical_or_value.datatype
                value = lexical_or_value.value

        elif isinstance(lexical_or_value, str) or isinstance(lexical_or_value, bytes):
            # passed a string
            # try parsing lexical form of datatyped literal
            value = _castLexicalToPython(lexical_or_value, datatype)
            if datatype is not None and datatype in _toPythonMapping:
                # datatype is a recognized datatype IRI:
                # https://www.w3.org/TR/rdf11-concepts/#dfn-recognized-datatype-iris
                dt_uri: URIRef = URIRef(datatype)
                checker = _check_well_formed_types.get(dt_uri, _well_formed_by_value)
                well_formed = checker(lexical_or_value, value)
                ill_typed = ill_typed or (not well_formed)
            if value is not None and normalize:
                _value, _datatype = _castPythonToLiteral(value, datatype)
                if _value is not None and _is_valid_unicode(_value):
                    lexical_or_value = _value

        else:
            # passed some python object
            value = lexical_or_value
            _value, _datatype = _castPythonToLiteral(lexical_or_value, datatype)

            _datatype = None if _datatype is None else URIRef(_datatype)

            datatype = rdflib.util._coalesce(datatype, _datatype)
            if _value is not None:
                lexical_or_value = _value
            if datatype is not None:
                lang = None

        if isinstance(lexical_or_value, bytes):
            lexical_or_value = lexical_or_value.decode("utf-8")

        if datatype in (_XSD_NORMALISED_STRING, _XSD_TOKEN):
            lexical_or_value = _normalise_XSD_STRING(lexical_or_value)

        if datatype in (_XSD_TOKEN,):
            lexical_or_value = _strip_and_collapse_whitespace(lexical_or_value)

        try:
            inst = str.__new__(cls, lexical_or_value)
        except UnicodeDecodeError:
            inst = str.__new__(cls, lexical_or_value, "utf-8")

        inst._language = lang
        inst._datatype = datatype
        inst._value = value
        inst._ill_typed = ill_typed

        return inst

### `` __pos__

    __pos__() -> Literal

Implements unary plus operation for Literals with numeric values.

Example

    # Applying unary plus to an integer Literal
    +Literal(1)
    # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # Applying unary plus to a negative integer Literal
    +Literal(-1)
    # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # Using a string with a datatype
    from rdflib.namespace import XSD
    +Literal("-1", datatype=XSD.integer)
    # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # This will raise a TypeError
    +Literal("1")
    # TypeError: Not a number; rdflib.term.Literal('1')

Source code in `rdflib/term.py`

    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080

    def __pos__(self) -> Literal:
        """Implements unary plus operation for Literals with numeric values.

        Example:
            ```python
            # Applying unary plus to an integer Literal
            +Literal(1)
            # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # Applying unary plus to a negative integer Literal
            +Literal(-1)
            # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # Using a string with a datatype
            from rdflib.namespace import XSD
            +Literal("-1", datatype=XSD.integer)
            # rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # This will raise a TypeError
            +Literal("1")
            # TypeError: Not a number; rdflib.term.Literal('1')
            ```
        """
        if isinstance(self.value, (int, long_type, float)):
            return Literal(self.value.__pos__())
        else:
            raise TypeError(f"Not a number; {self!r}")

### `` __reduce__

    __reduce__() -> Tuple[Type[Literal], Tuple[str, Union[str, None], Union[str, None]]]

Source code in `rdflib/term.py`

    780
    781
    782
    783
    784
    785
    786

    def __reduce__(
        self,
    ) -> Tuple[Type[Literal], Tuple[str, Union[str, None], Union[str, None]]]:
        return (
            Literal,
            (str(self), self.language, self.datatype),
        )

### `` __repr__

    __repr__() -> str

Source code in `rdflib/term.py`

    1752
    1753
    1754
    1755
    1756
    1757
    1758
    1759
    1760
    1761
    1762

    def __repr__(self) -> str:
        args = [str.__repr__(self)]
        if self.language is not None:
            args.append("lang=" + repr(self.language))
        if self.datatype is not None:
            args.append("datatype=" + repr(self.datatype))
        if self.__class__ == Literal:
            clsName = "rdflib.term.Literal"  # noqa: N806
        else:
            clsName = self.__class__.__name__  # noqa: N806
        return f"{clsName}({', '.join(args)})"

### `` __setstate__

    __setstate__(arg: Tuple[Any, Dict[str, Any]]) -> None

Source code in `rdflib/term.py`

    791
    792
    793
    794

    def __setstate__(self, arg: Tuple[Any, Dict[str, Any]]) -> None:
        _, d = arg
        self._language = d["language"]
        self._datatype = d["datatype"]

### `` __sub__

    __sub__(val: Any) -> Literal

Implements subtraction between Literals or between a Literal and a Python object.

Example

    from rdflib.namespace import XSD

    # Basic numeric subtraction
    Literal(2) - 1
    # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    Literal(1.1) - 1.0
    # rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'))

    Literal(1.1) - 1
    # rdflib.term.Literal('0.1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#decimal'))

    Literal(1.1, datatype=XSD.float) - Literal(1.0, datatype=XSD.float)
    # rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#float'))

    # This will raise a TypeError
    Literal("1.1") - 1.0
    # TypeError: Not a number; rdflib.term.Literal('1.1')

    Literal(1.1, datatype=XSD.integer) - Literal(1.0, datatype=XSD.integer)
    # rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

    # Handling dateTime/date/time based operations in Literals
    a = Literal('2006-01-01T20:50:00', datatype=XSD.dateTime)
    b = Literal('2006-02-01T20:50:00', datatype=XSD.dateTime)
    (b - a)
    # rdflib.term.Literal('P31D', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))

    a = Literal('2006-07-01T20:52:00', datatype=XSD.dateTime)
    b = Literal('2006-11-01T12:50:00', datatype=XSD.dateTime)
    (a - b)
    # rdflib.term.Literal('-P122DT15H58M', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))
    (b - a)
    # rdflib.term.Literal('P122DT15H58M', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))

Source code in `rdflib/term.py`

     903
     904
     905
     906
     907
     908
     909
     910
     911
     912
     913
     914
     915
     916
     917
     918
     919
     920
     921
     922
     923
     924
     925
     926
     927
     928
     929
     930
     931
     932
     933
     934
     935
     936
     937
     938
     939
     940
     941
     942
     943
     944
     945
     946
     947
     948
     949
     950
     951
     952
     953
     954
     955
     956
     957
     958
     959
     960
     961
     962
     963
     964
     965
     966
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
     986
     987
     988
     989
     990
     991
     992
     993
     994
     995
     996
     997
     998
     999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014

    def __sub__(self, val: Any) -> Literal:
        """Implements subtraction between Literals or between a Literal and a Python object.

        Example:
            ```python
            from rdflib.namespace import XSD

            # Basic numeric subtraction
            Literal(2) - 1
            # rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            Literal(1.1) - 1.0
            # rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'))

            Literal(1.1) - 1
            # rdflib.term.Literal('0.1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#decimal'))

            Literal(1.1, datatype=XSD.float) - Literal(1.0, datatype=XSD.float)
            # rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#float'))

            # This will raise a TypeError
            Literal("1.1") - 1.0
            # TypeError: Not a number; rdflib.term.Literal('1.1')

            Literal(1.1, datatype=XSD.integer) - Literal(1.0, datatype=XSD.integer)
            # rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

            # Handling dateTime/date/time based operations in Literals
            a = Literal('2006-01-01T20:50:00', datatype=XSD.dateTime)
            b = Literal('2006-02-01T20:50:00', datatype=XSD.dateTime)
            (b - a)
            # rdflib.term.Literal('P31D', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))

            a = Literal('2006-07-01T20:52:00', datatype=XSD.dateTime)
            b = Literal('2006-11-01T12:50:00', datatype=XSD.dateTime)
            (a - b)
            # rdflib.term.Literal('-P122DT15H58M', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))
            (b - a)
            # rdflib.term.Literal('P122DT15H58M', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))
            ```
        """
        # if no val is supplied, return this Literal
        if val is None:
            return self

        # convert the val to a Literal, if it isn't already one
        if not isinstance(val, Literal):
            val = Literal(val)

        if not getattr(self, "datatype"):
            raise TypeError(
                "Minuend Literal must have Numeric, Date, Datetime or Time datatype."
            )
        elif not getattr(val, "datatype"):
            raise TypeError(
                "Subtrahend Literal must have Numeric, Date, Datetime or Time datatype."
            )

        if (
            self.datatype in (_XSD_DATETIME, _XSD_DATE)
            and val.datatype in _TIME_DELTA_TYPES
        ):
            date1: Union[datetime, date] = self.toPython()
            duration: Union[Duration, timedelta] = val.toPython()
            difference = date1 - duration
            return Literal(difference, datatype=self.datatype)

        # if self is time based and value is duration
        elif self.datatype == _XSD_TIME and val.datatype in _TIME_DELTA_TYPES:
            selfv: time = self.toPython()
            valv: Union[Duration, timedelta] = val.toPython()
            sdt = datetime.combine(date(2000, 1, 1), selfv) - valv
            return Literal(sdt.time(), datatype=self.datatype)

        # if the datatypes are the same, just subtract the Python values and convert back
        if self.datatype == val.datatype:
            if self.datatype == _XSD_TIME:
                sdt = datetime.combine(date.today(), self.toPython())
                vdt = datetime.combine(date.today(), val.toPython())
                return Literal(sdt - vdt, datatype=_XSD_DURATION)
            else:
                return Literal(
                    self.toPython() - val.toPython(),
                    self.language,
                    datatype=(
                        _XSD_DURATION
                        if self.datatype in (_XSD_DATETIME, _XSD_DATE, _XSD_TIME)
                        else self.datatype
                    ),
                )

        # if the datatypes are not the same but are both numeric, subtract the Python values and strip off decimal junk
        # (i.e. tiny numbers (more than 17 decimal places) and trailing zeros) and return as a decimal
        elif (
            self.datatype in _NUMERIC_LITERAL_TYPES
            and val.datatype in _NUMERIC_LITERAL_TYPES
        ):
            return Literal(
                Decimal(
                    f"{round(Decimal(self.toPython()) - Decimal(val.toPython()), 15):f}".rstrip(
                        "0"
                    ).rstrip(
                        "."
                    )
                ),
                datatype=_XSD_DECIMAL,
            )
        # in all other cases, perform string concatenation
        else:
            raise TypeError(
                f"Cannot subtract a Literal of datatype {str(val.datatype)} from a Literal of datatype {str(self.datatype)}"
            )

### `` eq

    eq(other: Any) -> bool

Compare the value of this literal with something else.

This comparison can be done in two ways:

1. With the value of another literal - comparisons are then done in literal “value space” according to the rules of XSD subtype-substitution/type-promotion

2. With a Python object:

3. string objects can be compared with plain-literals or those with datatype xsd:string
4. bool objects with xsd:boolean
5. int, long or float with numeric xsd types
6. date, time, datetime objects with xsd:date, xsd:time, xsd:datetime

Any other operations returns NotImplemented.

Source code in `rdflib/term.py`

    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440
    1441
    1442
    1443
    1444
    1445
    1446
    1447
    1448
    1449
    1450
    1451
    1452
    1453
    1454
    1455
    1456
    1457
    1458
    1459
    1460
    1461
    1462
    1463
    1464
    1465
    1466
    1467
    1468
    1469
    1470
    1471
    1472
    1473
    1474
    1475
    1476
    1477
    1478
    1479
    1480
    1481
    1482
    1483
    1484
    1485
    1486
    1487
    1488
    1489
    1490
    1491
    1492
    1493
    1494
    1495
    1496
    1497
    1498
    1499
    1500
    1501
    1502
    1503
    1504
    1505
    1506
    1507
    1508
    1509
    1510
    1511
    1512
    1513
    1514
    1515
    1516
    1517
    1518
    1519
    1520
    1521
    1522
    1523
    1524
    1525
    1526

    def eq(self, other: Any) -> bool:
        """Compare the value of this literal with something else.

        This comparison can be done in two ways:

        1. With the value of another literal - comparisons are then done in literal "value space"
            according to the rules of XSD subtype-substitution/type-promotion

        2. With a Python object:
           * string objects can be compared with plain-literals or those with datatype xsd:string
           * bool objects with xsd:boolean
           * int, long or float with numeric xsd types
           * date, time, datetime objects with xsd:date, xsd:time, xsd:datetime

        Any other operations returns NotImplemented.
        """
        if isinstance(other, Literal):
            # Fast path for comparing numeric literals
            # that are not ill-typed and don't have a None value
            if (
                (
                    self.datatype in _NUMERIC_LITERAL_TYPES
                    and other.datatype in _NUMERIC_LITERAL_TYPES
                )
                and ((not self.ill_typed) and (not other.ill_typed))
                and (self.value is not None and other.value is not None)
            ):
                if self.value is not None and other.value is not None:
                    return self.value == other.value
                else:
                    if str.__eq__(self, other):
                        return True
                    raise TypeError(
                        # TODO: Should this use repr strings in the error message?
                        "I cannot know that these two lexical forms do not map to the "
                        f"same value: {self} and {other}"
                    )
            if (self.language or "").lower() != (other.language or "").lower():
                return False

            dtself = rdflib.util._coalesce(self.datatype, default=_XSD_STRING)
            dtother = rdflib.util._coalesce(other.datatype, default=_XSD_STRING)

            if dtself == _XSD_STRING and dtother == _XSD_STRING:
                # string/plain literals, compare on lexical form
                return str.__eq__(self, other)

            # XML can be compared to HTML, only if html5rdf is enabled
            if (
                (dtself in _XML_COMPARABLE and dtother in _XML_COMPARABLE)
                and
                # Ill-typed can be None if unknown, but we don't want it to be True.
                ((self.ill_typed is not True) and (other.ill_typed is not True))
                and (self.value is not None and other.value is not None)
            ):
                return _isEqualXMLNode(self.value, other.value)

            if dtself != dtother:
                if rdflib.DAWG_LITERAL_COLLATION:
                    raise TypeError(
                        f"I don't know how to compare literals with datatypes {self.datatype} and {other.datatype}"
                    )
                else:
                    return False

            # matching non-string DTs now - do we compare values or
            # lexical form first?  comparing two ints is far quicker -
            # maybe there are counter examples

            if self.value is not None and other.value is not None:
                return self.value == other.value
            else:
                if str.__eq__(self, other):
                    return True

                if self.datatype == _XSD_STRING:
                    return False  # string value space=lexical space

                # matching DTs, but not matching, we cannot compare!
                raise TypeError(
                    # TODO: Should this use repr strings in the error message?
                    "I cannot know that these two lexical forms do not map to the same "
                    f"value: {self} and {other}"
                )

        elif isinstance(other, Node):
            return False  # no non-Literal nodes are equal to a literal

        elif isinstance(other, str):
            # only plain-literals can be directly compared to strings

            # TODO: Is "blah"@en eq "blah" ?
            if self.language is not None:
                return False

            if self.datatype == _XSD_STRING or self.datatype is None:
                return str(self) == other

        elif isinstance(other, (int, long_type, float)):
            if self.datatype in _NUMERIC_LITERAL_TYPES:
                return self.value == other
        elif isinstance(other, (date, datetime, time)):
            if self.datatype in (_XSD_DATETIME, _XSD_DATE, _XSD_TIME):
                return self.value == other
        elif isinstance(other, (timedelta, Duration)):
            if self.datatype in (
                _XSD_DURATION,
                _XSD_DAYTIMEDURATION,
                _XSD_YEARMONTHDURATION,
            ):
                return self.value == other
        # NOTE for type ignore: bool is a subclass of int so this won't ever run.
        elif isinstance(other, bool):  # type: ignore[unreachable, unused-ignore]
            if self.datatype == _XSD_BOOLEAN:
                return self.value == other

        return NotImplemented

### `` n3

    n3(namespace_manager: Optional[NamespaceManager] = None) -> str

Returns a representation in the N3 format.

    >>> Literal("foo").n3()
    '"foo"'

Strings with newlines or triple-quotes:

    >>> Literal("foo\nbar").n3()
    '"""foo\nbar"""'
    >>> Literal("''\'").n3()
    '"\'\'\'"'
    >>> Literal('"""').n3()
    '"\\"\\"\\""'

Language:

    >>> Literal("hello", lang="en").n3()
    '"hello"@en'

Datatypes:

    >>> Literal(1).n3()
    '"1"^^<http://www.w3.org/2001/XMLSchema#integer>'
    >>> Literal(1.0).n3()
    '"1.0"^^<http://www.w3.org/2001/XMLSchema#double>'
    >>> Literal(True).n3()
    '"true"^^<http://www.w3.org/2001/XMLSchema#boolean>'

Datatype and language isn’t allowed (datatype takes precedence):

    >>> Literal(1, lang="en").n3()
    '"1"^^<http://www.w3.org/2001/XMLSchema#integer>'

Custom datatype:

    >>> footype = URIRef("http://example.org/ns#foo")
    >>> Literal("1", datatype=footype).n3()
    '"1"^^<http://example.org/ns#foo>'

Passing a namespace-manager will use it to abbreviate datatype URIs:

    >>> from rdflib import Graph
    >>> Literal(1).n3(Graph().namespace_manager)
    '"1"^^xsd:integer'

Source code in `rdflib/term.py`

    1531
    1532
    1533
    1534
    1535
    1536
    1537
    1538
    1539
    1540
    1541
    1542
    1543
    1544
    1545
    1546
    1547
    1548
    1549
    1550
    1551
    1552
    1553
    1554
    1555
    1556
    1557
    1558
    1559
    1560
    1561
    1562
    1563
    1564
    1565
    1566
    1567
    1568
    1569
    1570
    1571
    1572
    1573
    1574
    1575
    1576
    1577
    1578
    1579
    1580
    1581
    1582
    1583
    1584
    1585
    1586
    1587
    1588
    1589
    1590
    1591
    1592
    1593
    1594
    1595
    1596
    1597
    1598
    1599
    1600
    1601

    def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
        r'''Returns a representation in the N3 format.

        ```python
        >>> Literal("foo").n3()
        '"foo"'

        ```

        Strings with newlines or triple-quotes:

        ```python
        >>> Literal("foo\nbar").n3()
        '"""foo\nbar"""'
        >>> Literal("''\'").n3()
        '"\'\'\'"'
        >>> Literal('"""').n3()
        '"\\"\\"\\""'

        ```

        Language:

        ```python
        >>> Literal("hello", lang="en").n3()
        '"hello"@en'

        ```

        Datatypes:

        ```python
        >>> Literal(1).n3()
        '"1"^^<http://www.w3.org/2001/XMLSchema#integer>'
        >>> Literal(1.0).n3()
        '"1.0"^^<http://www.w3.org/2001/XMLSchema#double>'
        >>> Literal(True).n3()
        '"true"^^<http://www.w3.org/2001/XMLSchema#boolean>'

        ```

        Datatype and language isn't allowed (datatype takes precedence):

        ```python
        >>> Literal(1, lang="en").n3()
        '"1"^^<http://www.w3.org/2001/XMLSchema#integer>'

        ```

        Custom datatype:

        ```python
        >>> footype = URIRef("http://example.org/ns#foo")
        >>> Literal("1", datatype=footype).n3()
        '"1"^^<http://example.org/ns#foo>'

        ```

        Passing a namespace-manager will use it to abbreviate datatype URIs:

        ```python
        >>> from rdflib import Graph
        >>> Literal(1).n3(Graph().namespace_manager)
        '"1"^^xsd:integer'

        ```
        '''
        if namespace_manager:
            return self._literal_n3(qname_callback=namespace_manager.normalizeUri)
        else:
            return self._literal_n3()

### `` neq

    neq(other: Any) -> bool

Source code in `rdflib/term.py`

    1528
    1529

    def neq(self, other: Any) -> bool:
        return not self.eq(other)

### `` normalize

    normalize() -> Literal

Returns a new literal with a normalised lexical representation of this literal

    >>> from rdflib import XSD
    >>> Literal("01", datatype=XSD.integer, normalize=False).normalize()
    rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

Illegal lexical forms for the datatype given are simply passed on

    >>> Literal("a", datatype=XSD.integer, normalize=False)
    rdflib.term.Literal('a', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

Source code in `rdflib/term.py`

    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753

    def normalize(self) -> Literal:
        """
        Returns a new literal with a normalised lexical representation
        of this literal

        ```python
        >>> from rdflib import XSD
        >>> Literal("01", datatype=XSD.integer, normalize=False).normalize()
        rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

        ```

        Illegal lexical forms for the datatype given are simply passed on

        ```python
        >>> Literal("a", datatype=XSD.integer, normalize=False)
        rdflib.term.Literal('a', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))

        ```
        """

        if self.value is not None:
            return Literal(self.value, datatype=self.datatype, lang=self.language)
        else:
            return self

### `` toPython

    toPython() -> Any

Returns an appropriate python datatype derived from this RDF Literal

Source code in `rdflib/term.py`

    1764
    1765
    1766
    1767
    1768
    1769
    1770
    1771

    def toPython(self) -> Any:  # noqa: N802
        """
        Returns an appropriate python datatype derived from this RDF Literal
        """

        if self.value is not None:
            return self.value
        return self

## `` Node

Bases: `ABC`

A Node in the Graph.

Methods:

- `n3` –

Attributes:

- `__slots__` –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

### `` n3 `abstractmethod`

    n3(namespace_manager: Optional[NamespaceManager] = None) -> str

Source code in `rdflib/term.py`

    141
    142

    @abc.abstractmethod
    def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str: ...

## `` RDFLibGenid

Bases: `Genid`

Attributes:

- `__slots__` –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

## `` URIRef

Bases: `IdentifiedNode`

RDF 1.1’s IRI Section

Terminology

Documentation on RDF outside of RDFLib uses the term IRI or URI whereas this class is called URIRef. This is because it was made when the first version of the RDF specification was current, and it used the term URIRef, see RDF 1.0 URIRef

An IRI (Internationalized Resource Identifier) within an RDF graph is a Unicode string that conforms to the syntax defined in RFC 3987.

IRIs in the RDF abstract syntax MUST be absolute, and MAY contain a fragment identifier.

IRIs are a generalization of URIs [RFC3986] that permits a wider range of Unicode characters.

Methods:

- `__add__` –
- `__mod__` –
- `__new__` –
- `__radd__` –
- `__reduce__` –
- `__repr__` –
- `de_skolemize` –

  Create a Blank Node from a skolem URI, in accordance

- `defrag` –
- `n3` –

  This will do a limited check for valid URIs,

Attributes:

- `__invert__` (`Callable[[URIRef], InvPath]`) –
- `__neg__` (`Callable[[URIRef], NegatedPath]`) –
- `__or__` (`Callable[[URIRef, Union[URIRef, Path]], AlternativePath]`) –
- `__slots__` –
- `__truediv__` (`Callable[[URIRef, Union[URIRef, Path]], SequencePath]`) –
- `fragment` (`str`) –

  Return the URL Fragment

### `` __invert__ `instance-attribute`

    __invert__: Callable[[URIRef], InvPath]

### `` __neg__ `instance-attribute`

    __neg__: Callable[[URIRef], NegatedPath]

### `` __or__ `instance-attribute`

    __or__: Callable[[URIRef, Union[URIRef, Path]], AlternativePath]

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

### `` __truediv__ `instance-attribute`

    __truediv__: Callable[[URIRef, Union[URIRef, Path]], SequencePath]

### `` fragment `property`

    fragment: str

Return the URL Fragment

    >>> URIRef("http://example.com/some/path/#some-fragment").fragment
    'some-fragment'
    >>> URIRef("http://example.com/some/path/").fragment
    ''

### `` __add__

    __add__(other) -> URIRef

Source code in `rdflib/term.py`

    358
    359

    def __add__(self, other) -> URIRef:
        return self.__class__(str(self) + other)

### `` __mod__

    __mod__(other) -> URIRef

Source code in `rdflib/term.py`

    364
    365

    def __mod__(self, other) -> URIRef:
        return self.__class__(str(self) % other)

### `` __new__

    __new__(value: str, base: Optional[str] = None)

Source code in `rdflib/term.py`

    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305

    def __new__(cls, value: str, base: Optional[str] = None):
        if base is not None:
            ends_in_hash = value.endswith("#")
            # type error: Argument "allow_fragments" to "urljoin" has incompatible type "int"; expected "bool"
            value = urljoin(base, value, allow_fragments=1)  # type: ignore[arg-type]
            if ends_in_hash:
                if not value.endswith("#"):
                    value += "#"

        if not _is_valid_uri(value):
            logger.warning(
                f"{value} does not look like a valid URI, trying to serialize this will break."
            )

        try:
            rt = str.__new__(cls, value)
        except UnicodeDecodeError:
            # type error: No overload variant of "__new__" of "str" matches argument types "Type[URIRef]", "str", "str"
            rt = str.__new__(cls, value, "utf-8")  # type: ignore[call-overload]
        return rt

### `` __radd__

    __radd__(other) -> URIRef

Source code in `rdflib/term.py`

    361
    362

    def __radd__(self, other) -> URIRef:
        return self.__class__(other + str(self))

### `` __reduce__

    __reduce__() -> Tuple[Type[URIRef], Tuple[str]]

Source code in `rdflib/term.py`

    347
    348

    def __reduce__(self) -> Tuple[Type[URIRef], Tuple[str]]:
        return (URIRef, (str(self),))

### `` __repr__

    __repr__() -> str

Source code in `rdflib/term.py`

    350
    351
    352
    353
    354
    355
    356

    def __repr__(self) -> str:
        if self.__class__ is URIRef:
            clsName = "rdflib.term.URIRef"  # noqa: N806
        else:
            clsName = self.__class__.__name__  # noqa: N806

        return f"{clsName}({str.__repr__(self)})"

### `` de_skolemize

    de_skolemize() -> BNode

Create a Blank Node from a skolem URI, in accordance with http://www.w3.org/TR/rdf11-concepts/#section-skolemization. This function accepts only rdflib type skolemization, to provide a round-tripping within the system.

Added in version 4.0

Source code in `rdflib/term.py`

    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387

    def de_skolemize(self) -> BNode:
        """Create a Blank Node from a skolem URI, in accordance
        with http://www.w3.org/TR/rdf11-concepts/#section-skolemization.
        This function accepts only rdflib type skolemization, to provide
        a round-tripping within the system.

        Added in version 4.0
        """
        if isinstance(self, RDFLibGenid):
            parsed_uri = urlparse(f"{self}")
            return BNode(value=parsed_uri.path[len(rdflib_skolem_genid) :])
        elif isinstance(self, Genid):
            bnode_id = f"{self}"
            if bnode_id in skolems:
                return skolems[bnode_id]
            else:
                retval = BNode()
                skolems[bnode_id] = retval
                return retval
        else:
            raise Exception(f"<{self}> is not a skolem URI")

### `` defrag

    defrag() -> URIRef

Source code in `rdflib/term.py`

    326
    327
    328
    329
    330
    331

    def defrag(self) -> URIRef:
        if "#" in self:
            url, frag = urldefrag(self)
            return URIRef(url)
        else:
            return self

### `` n3

    n3(namespace_manager: Optional[NamespaceManager] = None) -> str

This will do a limited check for valid URIs, essentially just making sure that the string includes no illegal characters (`<, >, ", {, }, |, \,`, ^`)

Parameters:

- #### `namespace_manager`

  (`Optional[NamespaceManager]`, default: `None` ) –

  if not None, will be used to make up a prefixed name

Source code in `rdflib/term.py`

    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324

    def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
        """This will do a limited check for valid URIs,
        essentially just making sure that the string includes no illegal
        characters (`<, >, ", {, }, |, \\, `, ^`)

        Args:
            namespace_manager: if not None, will be used to make up a prefixed name
        """

        if not _is_valid_uri(self):
            raise Exception(
                f'"{self}" does not look like a valid URI, I cannot serialize this as N3/Turtle. Perhaps you wanted to urlencode it?'
            )

        if namespace_manager:
            return namespace_manager.normalizeUri(self)
        else:
            return f"<{self}>"

## `` Variable

Bases: `Identifier`

A Variable - this is used for querying, or in Formula aware graphs, where Variables can be stored

Methods:

- `__new__` –
- `__reduce__` –
- `__repr__` –
- `n3` –
- `toPython` –

Attributes:

- `__slots__` –

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ()

### `` __new__

    __new__(value: str)

Source code in `rdflib/term.py`

    2345
    2346
    2347
    2348
    2349
    2350

    def __new__(cls, value: str):
        if len(value) == 0:
            raise Exception("Attempted to create variable with empty string as name!")
        if value[0] == "?":
            value = value[1:]
        return str.__new__(cls, value)

### `` __reduce__

    __reduce__() -> Tuple[Type[Variable], Tuple[str]]

Source code in `rdflib/term.py`

    2366
    2367

    def __reduce__(self) -> Tuple[Type[Variable], Tuple[str]]:
        return (Variable, (str(self),))

### `` __repr__

    __repr__() -> str

Source code in `rdflib/term.py`

    2352
    2353
    2354
    2355
    2356
    2357
    2358

    def __repr__(self) -> str:
        if self.__class__ is Variable:
            clsName = "rdflib.term.Variable"  # noqa: N806
        else:
            clsName = self.__class__.__name__  # noqa: N806

        return f"{clsName}({str.__repr__(self)})"

### `` n3

    n3(namespace_manager: Optional[NamespaceManager] = None) -> str

Source code in `rdflib/term.py`

    2363
    2364

    def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
        return "?" + self

### `` toPython

    toPython() -> str

Source code in `rdflib/term.py`

    2360
    2361

    def toPython(self) -> str:  # noqa: N802
        return "?" + self

## `` bind

    bind(datatype: str, pythontype: Type[Any], constructor: Optional[Callable[[str], Any]] = None, lexicalizer: Optional[Callable[[Any], Union[str, bytes]]] = None, datatype_specific: bool = False) -> None

register a new datatype<->pythontype binding

Parameters:

- ### `constructor`

  (`Optional[Callable[[str], Any]]`, default: `None` ) –

  An optional function for converting lexical forms into a Python instances, if not given the pythontype is used directly

- ### `lexicalizer`

  (`Optional[Callable[[Any], Union[str, bytes]]]`, default: `None` ) –

  An optional function for converting python objects to lexical form, if not given object.str is used

- ### `datatype_specific`

  (`bool`, default: `False` ) –

  Makes the lexicalizer function be accessible from the pair (pythontype, datatype) if set to True or from the pythontype otherwise. False by default

Source code in `rdflib/term.py`

    2302
    2303
    2304
    2305
    2306
    2307
    2308
    2309
    2310
    2311
    2312
    2313
    2314
    2315
    2316
    2317
    2318
    2319
    2320
    2321
    2322
    2323
    2324
    2325
    2326
    2327
    2328
    2329
    2330
    2331
    2332
    2333
    2334

    def bind(
        datatype: str,
        pythontype: Type[Any],
        constructor: Optional[Callable[[str], Any]] = None,
        lexicalizer: Optional[Callable[[Any], Union[str, bytes]]] = None,
        datatype_specific: bool = False,
    ) -> None:
        """
        register a new datatype<->pythontype binding

        Args:
            constructor: An optional function for converting lexical forms
                into a Python instances, if not given the pythontype
                is used directly
            lexicalizer: An optional function for converting python objects to
                lexical form, if not given object.__str__ is used
            datatype_specific: Makes the lexicalizer function be accessible
                from the pair (pythontype, datatype) if set to True
                or from the pythontype otherwise. False by default
        """
        if datatype_specific and datatype is None:
            raise Exception("No datatype given for a datatype-specific binding")

        if datatype in _toPythonMapping:
            logger.warning(f"datatype '{datatype}' was already bound. Rebinding.")

        if constructor is None:
            constructor = pythontype
        _toPythonMapping[datatype] = constructor
        if datatype_specific:
            _SpecificPythonToXSDRules.append(((pythontype, datatype), lexicalizer))
        else:
            _GenericPythonToXSDRules.append((pythontype, (lexicalizer, datatype)))