what does this mean?

Discussion in 'Python' started by mr. ice, May 19, 2004.

  1. mr. ice

    mr. ice Guest

    I am new to python, The following statement is in the beginning of
    class definition in a python scripts, what does it mean? Help?
    class %(class_)s_W : public XMsg_W
    {


    icemon
     
    mr. ice, May 19, 2004
    #1
    1. Advertising

  2. mr. ice

    Peter Otten Guest

    mr. ice wrote:

    > I am new to python, The following statement is in the beginning of
    > class definition in a python scripts, what does it mean? Help?
    > class %(class_)s_W : public XMsg_W
    > {


    You are probably inside a string (if they start with """ or ''' they can go
    over multiple lines in python). The script seems to be meant to generate
    C++ source code.

    >>> print """

    .... class %(class_)s_W : public XMsg_W
    .... { // more C++ code
    .... }
    .... """ % {"class_": "MyFancyClass"}

    class MyFancyClass_W : public XMsg_W
    { // more C++ code
    }

    See how the "%(class_)s" thingy was replaced by the value "MyFancyClass"
    corresponding to the key "class_" in the dictionary? That's one way to use
    the % operator with strings.

    Peter
     
    Peter Otten, May 19, 2004
    #2
    1. Advertising

  3. mr. ice wrote:

    >I am new to python, The following statement is in the beginning of
    >class definition in a python scripts, what does it mean? Help?
    >class %(class_)s_W : public XMsg_W
    >{
    >
    >


    You will likely notice that there are "" or '' marks around the text
    you're reading there. It looks a lot like a string template that
    someone is using to create C++ code. A string like this "Hello
    %(name)s_xyz" gets expanded in Python like so:

    name = 'this'
    value = "Hello %(name)s_xyz" % locals()

    yielding:

    value == "Hello this_xyz"

    So whoever coded the module is likely trying to auto-generate some C++
    code using Python string substitution.

    HTH,
    Mike


    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://members.rogers.com/mcfletch/
    blog: http://zope.vex.net/~mcfletch/plumbing/
     
    Mike C. Fletcher, May 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pratip Mukherjee

    Help: what does this VHDL code mean?

    Pratip Mukherjee, Jun 22, 2005, in forum: VHDL
    Replies:
    16
    Views:
    1,310
    Kim Enkovaara
    Jun 27, 2005
  2. methi
    Replies:
    1
    Views:
    906
    info_
    Jun 23, 2005
  3. Li Ma
    Replies:
    1
    Views:
    2,368
    Roedy Green
    Mar 9, 2009
  4. Rahul
    Replies:
    4
    Views:
    627
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,286
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page