print - parameters ?

  • Thread starter Helmut Jarausch
  • Start date
H

Helmut Jarausch

Hi,
sorry for this probably very simple question.

How can I build a parameter list for 'print' ?

Background:
I'd like to write a function like

def myprint(Msg,*Args) :
print (Msg,)+Args

when called as

myprint(MyMsg,x)

it should be equivalent to

print MyMsg,x

but, e.g.

class ABC :
def __str__(self) :
return "ABC-class"

x=ABC()

myprint('this is',x)

gives

('this is', <__main__.ABC instance at 0x4084fc8c>)

instead of

this is ABC-class

What am I missing (what sort of thing is the parameter list of print) ?
Many thanks for a hint,


Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
A

Alex Martelli

Helmut Jarausch said:
Hi,
sorry for this probably very simple question.

How can I build a parameter list for 'print' ?

You can't really do that, since (alas) it's a statement, not a function.
Background:
I'd like to write a function like

def myprint(Msg,*Args) :
print (Msg,)+Args

def myprint(Msg, *Args):
print Msg,
for arg in Args: print arg,
print
myprint('this is',x)

gives

('this is', <__main__.ABC instance at 0x4084fc8c>)

instead of

this is ABC-class

You're printing a tuple, and that uses repr on each item, with commas to
separate and parentheses around it all.

What am I missing (what sort of thing is the parameter list of print) ?

It's not really a "sort of thing", alas... print is a statement so it
lives by its own rules (a Python wart, Guido's admitted that; maybe it
will change in Python 3000... let's hope!).


Alex
 
P

Peter Abel

Helmut Jarausch said:
Hi,
sorry for this probably very simple question.

How can I build a parameter list for 'print' ?

Background:
I'd like to write a function like

def myprint(Msg,*Args) :
print (Msg,)+Args

when called as

myprint(MyMsg,x)

it should be equivalent to

print MyMsg,x

but, e.g.

class ABC :
def __str__(self) :
return "ABC-class"

x=ABC()

myprint('this is',x)

gives

('this is', <__main__.ABC instance at 0x4084fc8c>)

instead of

this is ABC-class

What am I missing (what sort of thing is the parameter list of print) ?
Many thanks for a hint,


Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

As Alex pointed out there is a tiny difference
between __repr__ and __str__ but with an eminent effect
see the following examples:

case 1:
-------.... def __str__(self):
.... return "ABC-class"
.... '<__main__.ABC instance at 0x00DFA640>'


case 2:
-------.... def __repr__(self):
.... return "ABC-class"
.... def __str__(self):
.... return "ABC-class"
....

So for both cases the following function works:
.... format=''.join(['%s']*(len(Args)+1))
.... print format % tuple([Msg]+map(str,Args))
....
.... def __str__(self):
.... return "ABC-class"
.... this is ABC-class
.... def __repr__(self):
.... return "ABC-class"
.... def __str__(self):
.... return "ABC-class"
....
BTW the need of the statement

.... print format % tuple([Msg]+map(str,Args))

was new for me too.

Regards
Peter
 
H

Helmut Jarausch

Alex Martelli wrote:
......
It's not really a "sort of thing", alas... print is a statement so it
lives by its own rules (a Python wart, Guido's admitted that; maybe it
will change in Python 3000... let's hope!).

Is there any chance that Python 3000 would allow an alternate function call
in addition to the standard function call
I'd suggest something along the lines of a TeX macro definition, e.g.

def integrate(x,y,z) accepts function 'from' float 'to' float

this must then be called as

from math import sin
.....
integrate sin from 0.0 to 3.14

Note, the keywords 'from' and 'to' must be present.

It would still be possible to called it as
integrate(sin,0.0,3.14)

the general rule would be
def <function_name>(<argument_list>) accepts <type> ([<keyword-string>] <type>)*

Then 'print' could just be a standard function.

Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
V

Ville Vainio

Helmut> Is there any chance that Python 3000 would allow an
Helmut> alternate function call in addition to the standard
Helmut> function call

Helmut> I'd suggest something along the lines of a TeX macro
Helmut> definition, e.g.

Helmut> def integrate(x,y,z) accepts function 'from' float 'to' float

Helmut> this must then be called as

Helmut> from math import sin
Helmut> ....
Helmut> integrate sin from 0.0 to 3.14

I'd venture to guess that there is no chance of that happening in
Python 3000.

"There should be one-- and preferably only one --obvious way to do it."
 
A

Alex Martelli

Helmut Jarausch said:
Alex Martelli wrote:
.....

Is there any chance that Python 3000 would allow an alternate function call
in addition to the standard function call

I suspect the chance is smaller than the proverbial snowball's in hell,
but surely there must be some, infinitesimal though it might be (e.g.,
aliens _could_ abduct the real Guido and replace him with a craftily
programmed droid). Apart from the issue of being able to freely mix
keywords like 'from' and plain identifiers like 'to' in exactly the same
role (a big mess in itself -- let's just assume it away), calling:

integrate(sin, from=0.0, to=3.14)

is already possible today, and the ability to remove the punctuation to
give your desired:
integrate sin from 0.0 to 3.14

is a kind of "let's add yet another syntax sugar possibility to the mix"
direction which it definitely _doesn't_ look like the trend for Python
3000 (simplification, removal of legacy issues, stronger emphasis on
"there should ideally be only one way", look more like that trend).
Then 'print' could just be a standard function.

If you mean "with today's syntax", no it couldn't. The print statement
has this weird, unique syntax whereby it can have a trailing comma in a
meaningful sense, meaning "emit no endline here". Not to mention the
ever-lovin' "print>>BAH,buh"... Were it not for those warty quirks, any
current use of, e.g.:
print a, b, c
could already be identically translated to
printfunc(a, b, c)
with (net of softspace issues but just for brevity):
def printfunc(*args):
sys.stdout.write(' '.join(map(str,args))+'\n')

The parentheses aren't the big deal, and your proposed syntax for
alternate function calls (with those 'keywords' corresponding to nothing
like print uses today) doesn't seem as if it would help much here,
anyway...


Alex
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top