Top Description Fields Constructors Methods
org.python.core

public abstract Class PyFunction<C extends org.python.core.PyCode>

extends AbstractPyObject
implements DictPyObject
Class Inheritance
All Implemented Interfaces
org.python.core.DictPyObject, org.python.core.CraftedPyObject
Known Direct Subclasses
org.python.core.CPython311Function
Type Parameters
<C>
implementing class of code object
Imports
java.lang.invoke.MethodHandles, java.util.Collection, .Map, org.python.core.Exposed.FrozenArray, .Exposed.Member, .PyType.Flag

Python function object as created by a function definition and subsequently called.

Field Summary

Modifier and TypeField and Description
pack-priv PyDict
annotations

The __annotations__ attribute, a dict or null.

pack-priv final Object
builtins

The read-only __builtins__ attribute is often a dict but may be any object.

protected PyCell[]
closure

The read-only __closure__ attribute, or null.

protected C
code

The __code__ attribute: a code object, which is writable, but only with the right implementation type for the concrete class of the function.

protected Object[]
defaults

The (positional) __defaults__ or null.

private PyDict
dict

The __dict__ attribute, a dict or null.

pack-priv Object
doc

The __doc__ attribute, can be set to anything or null.

pack-priv final PyDict
globals

The read-only __globals__ attribute is a dict: other mappings won't do.

pack-priv final Interpreter
interpreter

The interpreter that defines the import context.

protected PyDict
kwdefaults

The __kwdefaults__ or null.

pack-priv Object
module

The __module__ attribute, can be anything or null meaning None

pack-priv String
name

The function name (__name__ attribute).

private String
qualname

The function qualified name (__qualname__ attribute).

pack-priv static final PyType
TYPE

The type of Python object this class implements.

Constructor Summary

AccessConstructor and Description
pack-priv
PyFunction(Interpreter
providing the module context not null
interpreter
,
C
to execute not null
code
,
PyDict
name space to treat as global variables not null
globals
,
Object[]
default positional argument values or null
defaults
,
PyDict
default keyword argument values or null
kwdefaults
,
Object
type annotations (dict, null or maybe tuple)
annotations
,
PyCell[]
variables referenced but not defined here, must be size expected by code or null if empty.
closure
)

Create a PyFunction supplying most of the attributes at construction time.

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract Object

Returns:

the return from the call
__call__
(Object[]
all the arguments (position then keyword)
args
,
String[]
of the keyword arguments (or null)
names
)

Canonical __call__ slot with Jython conventions, making function implementations callable.

private Object
private Object
pack-priv abstract PyFrame<? extends C>

Returns:

the frame
createFrame
(Object
name space to treat as local variables
locals
)

Create a PyFrame that will execute this PyFunction.

private Object

Returns:

the __builtins__ of the function
getBuiltinsFromGlobals
()

Look in __globals__ then the interpreter to find the container of built-in objects.

public Map<Object, Object>
getDict()

Implements org.python.core.DictPyObject.getDict.

The dictionary of the instance, (not necessarily a Python dict or writable.
pack-priv Interpreter

Returns:

interpreter that defines the import context
getInterpreter
()

Get the interpreter that defines the import context, which was current when this function was defined.

public PyType
getType()

Overrides org.python.core.AbstractPyObject.getType.

Implements org.python.core.CraftedPyObject.getType.

The Python type of this object.
public String
toString()

Overrides org.python.core.AbstractPyObject.toString.

Returns a string representation of the object.

Field Detail

annotationsback to summary
pack-priv PyDict annotations

The __annotations__ attribute, a dict or null.

builtinsback to summary
pack-priv final Object builtins

The read-only __builtins__ attribute is often a dict but may be any object. It will be accessed using the Python mapping protocol by the interpreter, at which point an error may be raised. Not null.

Annotations
@Member:__builtins__
readonly:true
closureback to summary
protected PyCell[] closure

The read-only __closure__ attribute, or null. See __closure__ access method

Annotations
@FrozenArray
codeback to summary
protected C code

The __code__ attribute: a code object, which is writable, but only with the right implementation type for the concrete class of the function. Not null.

defaultsback to summary
protected Object[] defaults

The (positional) __defaults__ or null.

dictback to summary
private PyDict dict

The __dict__ attribute, a dict or null.

docback to summary
pack-priv Object doc

The __doc__ attribute, can be set to anything or null.

Annotations
@Member:__doc__
globalsback to summary
pack-priv final PyDict globals

The read-only __globals__ attribute is a dict: other mappings won't do. Not null.

Annotations
@Member:__globals__
readonly:true
interpreterback to summary
pack-priv final Interpreter interpreter

The interpreter that defines the import context. Not null.

kwdefaultsback to summary
protected PyDict kwdefaults

The __kwdefaults__ or null.

moduleback to summary
pack-priv Object module

The __module__ attribute, can be anything or null meaning None

Annotations
@Member:__module__
nameback to summary
pack-priv String name

The function name (__name__ attribute).

qualnameback to summary
private String qualname

The function qualified name (__qualname__ attribute).

TYPEback to summary
pack-priv static final PyType TYPE

The type of Python object this class implements.

Constructor Detail

PyFunctionback to summary
pack-priv PyFunction(Interpreter interpreter, C code, PyDict globals, Object[] defaults, PyDict kwdefaults, Object annotations, PyCell[] closure)

Create a PyFunction supplying most of the attributes at construction time.

The strongly-typed defaults, kwdefaults, annotations and closure may be null if they would otherwise be empty. annotations is always exposed as a dict, but may be presented to the constructor as a dict or tuple of keys and values (or null).

Implementation Note

We differ from CPython in requiring a reference to the interpreter as an argument. Also, we favour a constructor in which the attributes are supplied defaults, kwdefaults, annotations and closure rather than added after construction.

Parameters
interpreter:Interpreter

providing the module context not null

code:C

to execute not null

globals:PyDict

name space to treat as global variables not null

defaults:Object[]

default positional argument values or null

kwdefaults:PyDict

default keyword argument values or null

annotations:Object

type annotations (dict, null or maybe tuple)

closure:PyCell[]

variables referenced but not defined here, must be size expected by code or null if empty.

Method Detail

__call__back to summary
pack-priv abstract Object __call__(Object[] args, String[] names) throws Throwable

Canonical __call__ slot with Jython conventions, making function implementations callable.

Parameters
args:Object[]

all the arguments (position then keyword)

names:String[]

of the keyword arguments (or null)

Returns:Object

the return from the call

Exceptions
Throwable:
for errors raised in the function
__repr__back to summary
private Object __repr__()
Annotations
@SuppressWarnings:unused
__str__back to summary
private Object __str__()
Annotations
@SuppressWarnings:unused
createFrameback to summary
pack-priv abstract PyFrame<? extends C> createFrame(Object locals)

Create a PyFrame that will execute this PyFunction. This frame should be "loose": PyFrame#back should be null and it should not be on any thread's stack.

Parameters
locals:Object

name space to treat as local variables

Returns:PyFrame<? extends C>

the frame

getBuiltinsFromGlobalsback to summary
private Object getBuiltinsFromGlobals()

Look in __globals__ then the interpreter to find the container of built-in objects.

Returns:Object

the __builtins__ of the function

getDictback to summary
public Map<Object, Object> getDict()

Implements org.python.core.DictPyObject.getDict.

Doc from org.python.core.DictPyObject.getDict.

The dictionary of the instance, (not necessarily a Python dict or writable. If the returned Map is not writable, it should throw a Java UnsupportedOperationException on attempts to modify it.

Returns:Map<Object, Object>

a mapping to treat like a dictionary (not null).

Annotations
@Override
getInterpreterback to summary
pack-priv Interpreter getInterpreter()

Get the interpreter that defines the import context, which was current when this function was defined. Not null.

Returns:Interpreter

interpreter that defines the import context

getTypeback to summary
public PyType getType()

Overrides org.python.core.AbstractPyObject.getType.

Implements org.python.core.CraftedPyObject.getType.

Doc from org.python.core.CraftedPyObject.getType.

The Python type of this object.

Returns:PyType

type of this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides org.python.core.AbstractPyObject.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override