Python UML Metamodel

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jan 30 13:30:23 EST 2008


En Tue, 29 Jan 2008 21:25:26 -0200, sccs cscs <zorg724 at yahoo.fr> escribió:

> I find an OPEN SOURCE tool  (http://bouml.free.fr/) that Recently  
> generates Python code from UML model.

Does it keep the model synchronized when you modify the Python code?

> I like to model the Python language metamodel himself, with it, e.g  the  
> model of the language: I need that to better understand the language  
> constraint of  the language.
>
> for example, i like to model that :
> -a  class "class" may inherit from 0..* class

1..* classes if you model new-style classes only. Classic classes  
disappear completely in Python 3.0

> -a class "class" is create from a class that is its "metaclass"
> -a class  "class" has 0..n attributes and 0..n method

Just attributes. Methods are created on-the-fly when the corresponding  
function attribute is retrieved from the instance.
And remember that instances are not restricted to what their class define.  
You can add or remove any attribute (even methods!) to any instance of a  
user-defined class at any time. (This doesn't apply to most builtin types,  
but *does* apply if you inherit from it)

> -a class "module" has 0..n class "class"

Not true. A module instance can contain any number of attributes of any  
type; classes are just an example.

> Does anyone know a document that describes it already, because I think  
> it is complicated to find this information in the documentation of  
> Python.

See section 2 "Data Model" and section 3 "Execution Model" in the Python  
Language Reference http://docs.python.org/ref/

> For example, can i say that:
>
> -a class  "class" has 0..n properties ?
> It seems that the Python metamodel is not perfect, because I do not find  
> attribute which give me the property list with a code like:
> "myPropertyList = myclass.properties"

dir(obj) retrieves most of the obj attributes. Some are hard to enumerate  
(if they are simulated in code, using __getattr__ by example), and some  
internal fields are not shown in dir() yet (the __dir__ method in 2.6  
would help in this case).

> - a class "method" can contains nested "method", but what is the way to  
> get a list of internal methods, without use ? Can i just write:
> "myNestedMethodList = method.nestedMethodList"

Are you talking about nested functions?
You can't enumerate them from outside the container function: Python is a  
dynamic language, those inner functions are created when the def statement  
is *executed*. If you have an if statement around the def, the function  
may not even exist.

> I think a metamodel Python would be welcome to complement the BNF  
> (http://docs.python.org/ref/grammar.txt), so as to know fully understand  
> the relationships between the various elements of language.

Maybe, but I think it's a lot simpler than you imagine. For the builtin  
types, see section 3.2 on the reference above; more info in the Library  
Reference.

-- 
Gabriel Genellina




More information about the Python-list mailing list