This will be a short article about Python dunder methods, also known as magic methods.
Dunder methods are special methods in Python that start and end with double underscores (__). They allow you to define the behavior of your objects for built-in operations, such as addition, subtraction, string representation, and more.
Some common dunder methods include:
__init__(self, ...): Called when an object is created.- Like our
%OnNewmethod in ObjectScript.
- Like our
__str__(self): Called by thestr()built-in function andprintto represent the object as a string.__repr__(self): Called by therepr()built-in function to represent the object for debugging.__add__(self, other): Called when the+operator is used.__len__(self): Called by thelen()built-in function to return the length of the object.__getitem__(self, key): Called to retrieve an item from a collection using the indexing syntax.__setitem__(self, key, value): Called to set an item in a collection using the indexing syntax.- ... and many more.
In ObjectScript, we don't have the sugar syntax like in Python, but we can achieve similar behavior using dunder methods.
Example, we have imported a Python module, it has a function that returns a python list, and we want to use it in ObjectScript. We must use the __getitem__ dunder method to access the items in the list.
# src/python/article/dunder_example.py
def get_list():
return [1, 2, 3, 4, 5]Class Article.DunderExample Extends %RegisteredObject
{
ClassMethod Run()
{
Set sys = ##class(%SYS.Python).Import("sys")
do sys.path.append("/irisdev/app/src/python/article")
set dunderExample = ##class(%SYS.Python).Import("dunder_example")
set myList = dunderExample."get_list"()
for i=0:1:myList."__len__"()-1 {
write myList."__getitem__"(i), !
}
}
}Let's run it:
iris session iris -U IRISAPP '##class(Article.DunderExample).Run()'This will output:
1
2
3
4
5
This demonstrates how to use dunder methods to interact with Python objects in an IRIS context, allowing you to leverage Python's capabilities while working within the ObjectScript environment.
A good use of dunder is to put at the end of your python script a if __name__ == "__main__": block to prevent the code from being executed when the script is imported as a module.
Remember, the first article explained that when you import a script, the code is executed. This block allows you to define code that should only run when the script is executed directly, not when it's imported.
Example:
# src/python/article/dunder_example.py
def get_list():
return [1, 2, 3, 4, 5]
if __name__ == "__main__":
print(get_list())What you can do in python even with it's sugar syntax, you can do it in ObjectScript with dunder methods.
