Constant functions

class sage.misc.constant_function.ConstantFunction

Bases: sage.structure.sage_object.SageObject

A class for function objects implementing constant functions.


sage: f = ConstantFunction(3)
sage: f
The constant function (...) -> 3
sage: f()
sage: f(5)

Such a function could be implemented as a lambda expression, but this is not (currently) picklable:

sage: g = lambda x: 3
sage: g == loads(dumps(g))
Traceback (most recent call last):
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
sage: f == loads(dumps(f))

Also, in the long run, the information that this function is constant could be used by some algorithms.


  • Should constant functions have unique representation?
  • Should the number of arguments be specified in the input?
  • Should this go into sage.categories.maps? Then what should be the parent (e.g. for lambda x: True)?


These tests do fail if we try to use UniqueRepresentation:

sage: f = ConstantFunction(True)
sage: g = ConstantFunction(1)
sage: f(), g()
(True, 1)

That’s because 1 and True cannot be distinguished as keys in a dictionary (argl!):

sage: { 1: 'a', True: 'b' }
{1: 'b'}

Previous topic

Temporary file handling

Next topic

Sage package management commands

This Page