On Jan 9, 2011, at 12:32 AM, Carey Gagnon wrote:

> As I'm still learning python, I'm confused by something. I've set up a
> virtual field with the following method:
> 
>    def getExtrasNumber(self):
>        extrasID = self.Record.id
>        extrasType = self.Record.type
>        extrasCreated = self.Record.datecreated
>        extrasYear = extrasCreated.strftime('%y')
>        return extrasType, extrasYear, extrasID
>        #print extrasType, extrasYear, extrasID
> 
> 
> with the return statement I get:
> (u'DEX', '11', 4L)
> 
> with the print statement I get:
> DEX 11 - 4
> 
> I want the return statement to format the output like the print statement
> Can anyone explain?

        Jacek gave you the code you need; let me explain the difference.

        In the return statement, you are defining a tuple to be returned. One 
thing many don't know is that in a tuple, the parentheses are optional! To see 
this, open a Python session, and type:

>>> 1,2,3
(1, 2, 3)

        Note that even though I typed those numbers without parentheses and 
without spaces after the commas, Python interpreted what I entered as the tuple 
(1, 2, 3). It is actually the comma that defines a tuple; the parentheses are 
for readability and explicitness:

>>> 1
1
>>> 1,
(1,)

        'print', on the other hand, is a built-in Python statement that accepts 
a comma-separated list of expressions and writes them to stdout. When you 
execute the print statement, it is evaluating each expression, converting to a 
string, and then writing it to stdout.


-- Ed Leafe



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to