Book a Demo

Author Topic: Python "self"  (Read 2435 times)

derek73

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Python "self"
« on: March 14, 2006, 01:50:27 pm »
Python's grossly retarded requirement for declaring "self" at the beginning of each object method has long been one of my peeves about the language.  What this means for code generation is I either have to specify "self" as I create behaviors (in which case, generation in any other language will be broken), or I have to modify the Parameter Implementation and Declaration templates in the Code Templates.

At first I tried putting "self," (no quotes) before the $params variable, but of course anything with an empty list of params has a dangling comma (,).  How do I modify the Python template to insert "self ," in front of non-empty parameter lists?

Also, is there a variable that can be referenced in the templates to differentiate class methods (which must NOT have a "self", but a "cls" instead) from object methods (which require self)?

How does one use round-trip engineering properly while working around this stupid language requirement?

Thank you all in advance!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Python "self"
« Reply #1 on: March 14, 2006, 03:51:28 pm »
Try this.

Code: [Select]
$dec=%REPLACE(opTag:"decorators","@","\n@")%
%TRIM($dec)%
%PI=" "%
def
%PI=""%
%if opScope == "Private"%
__
%endIf%
%opName%
$params = %list="Parameter" @separator=", "%
%if opStatic!="T"%
$self="self"
%if $params != ""%
$self+=", "
%endIf%
%endIf%
($self$params):


Although, it won't work properly for synchronisation.
« Last Edit: March 14, 2006, 03:53:54 pm by simonm »

derek73

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Python "self"
« Reply #2 on: March 14, 2006, 05:20:28 pm »
Very good, that will do it!  Thank you.

Now to finish straightening out my mess with __init__.py files and class inheritance...