| ) |
__init__xattributes__, __init__argslen__,
__object__xattributes__ and __object__xmethods__.
| ) |
name is defined for the classes which inherit
XNamedObject. More-over, the name is initialized at
a value which ``makes sense'' in the user python script.
Indeed, the name is found by the code calling the class.
Technically, it is not done by parsing the code source but by disassembling
the code itself (see python module dis).
Using XNamedObject avoid to write code like a = A(name='a').
For instance:
# --
# Copyright (C) CEA, EDF
# Author : Erwan ADAM (CEA)
# --
import unittest
from xdata import *
class A(XNamedObject):
pass
class ATestCase(unittest.TestCase):
def test(self):
a = A()
self.failUnlessEqual(a.getName(), "a")
self.failUnlessEqual(a.name, "a")
a.name = "aaa"
self.failUnlessEqual(a.getName(), "aaa")
self.failUnlessEqual(a.name, "aaa")
return
pass
if __name__ == '__main__':
unittest.main()
pass
If the name cannot be found, an exception is not raised at the object creation but if one try to access the name:
# --
# Copyright (C) CEA, EDF
# Author : Erwan ADAM (CEA)
# --
import unittest
from xdata import *
class A(XNamedObject):
pass
def f(x):
return x
class ATestCase(unittest.TestCase):
def test(self):
x = f(A())
# Obsolete : getName() does not raise but return None
# self.failUnlessRaises(XAttributeError, x.getName)
# self.failUnlessRaises(XAttributeError, getattr, x, "name")
return
pass
if __name__ == '__main__':
unittest.main()
pass