PyGlove
PyGlove is a general-purpose Python library for manipulating Python objects, introducing symbolic object-oriented programming (SOOP). It facilitates meta-program writing and is used in complex machine learning scenarios like AutoML, evolutionary computing, and daily programming tasks by providing a mutable symbolic object model and a rich set of object manipulation operations. It is currently at version 0.4.5 and actively maintained with regular updates.
Warnings
- breaking When overriding `pg.Object.__init__`, it is now mandatory to apply the `@pg.explicit_method_override` decorator. Failing to do so will result in an error, as it prevents accidental overrides of core PyGlove-managed methods.
- deprecated Several internal APIs were renamed. `pg.Object.schema` became `pg.Object.__schema__`, `pg.Object.type_name` became `pg.Object.__type_name__`, and `pg.Object.serialization_key` moved to `pg.JSONConvertible.__serialization_key__`. Similarly, `pg.Functor.signature` was renamed to `pg.Functor.__signature__`. Additionally, `pg.ContextualValue`, `pg.symbolic.GetAttributeContext`, and related methods were removed.
- gotcha PyGlove operates on a mutable programming paradigm, which can introduce complexities and limitations, especially when refactoring existing code. Adapting models and trainers to conform to PyGlove's symbolic object constraints, particularly for generic subclassed models, can be more challenging than expected, as symbolic nodes constructed by initialization parameters might not behave as anticipated.
- deprecated The `pg.members` function and its alias `pg.schema` were replaced by symbolic member declaration using field annotations as the recommended approach for defining symbolic fields within classes.
Install
-
pip install pyglove
Imports
- pyglove
import pyglove as pg
Quickstart
import pyglove as pg
@pg.symbolize
class Hello:
def __init__(self, subject):
self._greeting = f'Hello, {subject}!'
def greet(self):
print(self._greeting)
hello = Hello('World')
hello.greet()