Implementing Python Methods
This documentation contains Python-specific nuances to implementing C3 AI methods. If you are not familiar with the general concept of implementing C3 AI methods, refer to the documentation on Implementing C3 AI methods. If you are not familiar with the concept of creating methods using the C3 AI Type System, refer to the documentation on methods.
Interfacing with C3 AI
Within an implementation file for a Type, you have access to the global variable c3. This global variable is the namespace of all Types. Types can be accessed from this namespace, Types can be instantiated, and fields and methods can be accessed from instances of a Type.
For example:
def someFunc(...):
Obj = c3.Obj # Access Type `Obj`
objInst = c3.Obj() # Make an `Obj` instance
objInst2 = c3.Obj.make() # invoke static method `Obj#make` to make an `Obj` instance
...For more information, refer to the Implementing Type Methods documentation.
Python function signature
This section documents the requirements and best practices for the function signature of a Python function implementing a using the C3 AI Type System.
First argument
Python functions implementing methods in the C3 AI Type System must have a specific first positional argument.
Static methods
The first parameter of any python function implementing a C3 AI static non-member method must be cls. This can be the Type upon which this method was invoked.
Member methods
The first parameter of any python function implementing a C3 AI member method must be this. This can be the Type instance upon which this method was invoked.
Examples
Below is an example Type Foo and corresponding correct function signatures
type Foo {
staticFunc: function(): int py
memberFunc: member function(): int py
}def staticFunc(cls):
return 2
def memberFunc(this):
return 2Best practices
This section documents best practices when implementing C3 AI methods.
Do not use is for C3 AI values
Python's builtin is operator compares that two values have the same id. This is not a safe operation to use on C3 AI Obj, and may have non-deterministic behavior. It is recommended you instead use ==.
Import third party libraries inside functions
Unless you are leveraging Action Requirement File Extensions, importing third party libraries in the top level of your Python file is dangerous.
Consider the following example:
type Foo {
boringFunc: function(): int py-no-ml
fancyFunc: function(): int py-data-science
}import numpy as np
def boringFunc(cls):
return 2
def fancyFunc(cls):
return np.doSomething()Suppose py-data-science contains numpy and py-no-ml does not. Calling Foo#boringFunc from runtime py-no-ml raises an exception, since compiling the whole file containing Foo#boringFunc in runtime py-no-ml upon calling Foo#boringFunc. The runtime py-no-ml does not have the module numpy, so the line import numpy as np will throw an exception.
One easy way to resolve this issue is moving the import of numpy into fancyFunc:
def boringFunc(cls):
return 2
def fancyFunc(cls):
import numpy as np
return np.doSomething()