Emulate a printf() C-statement in Python???

M

Mr. Z

I'm trying emulate a printf() c statement that does, for example

char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);

In other words, printf() has a variable arguement list the we
all know.

I'm trying to do this in Python...

class MyPrintf(object):
# blah, blah
def myprintf(object, *arg):
# Here I'll have to know I NEED 2 arguments in format string
arg[0]
print arg[0] % (arg[1], arg[2])

name="Chris"
age=30
printf=MyPrintf()
printf.myPrintf(("My name is %s and I am %d years old.", name, age)
will of course print...
My name is Chris and I am 42 years old.

But
printf.myPrintf(("My name is %s.", name)
of course gives....
Index error: list index out of range

How can I generalize the print call in the myprintf() function to do this?

print arg[0] % (arg[1])
print arg[0] % (arg[1], arg[2])
print arg[0] % (arg[1], ..., arg[n])
 
C

Chris Rebert

I'm trying emulate a printf() c statement that does, for example

char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);

In other words, printf() has a variable arguement list the we
all know.

I'm trying to do this in Python...

class MyPrintf(object):
   # blah, blah
    def myprintf(object, *arg):
         # Here I'll have to know I NEED 2 arguments in format string
arg[0]
         print arg[0] % (arg[1], arg[2])

name="Chris"
age=30
printf=MyPrintf()
printf.myPrintf(("My name is %s and I am %d years old.", name, age)
will of course print...
My name is Chris and I am 42 years old.

But
printf.myPrintf(("My name is %s.", name)
of course gives....
Index error: list index out of range

How can I generalize the print call in the myprintf() function to do this?

print arg[0] % (arg[1])
print arg[0] % (arg[1], arg[2])
print arg[0] % (arg[1], ..., arg[n])

def printf(format, *args):
print format % args

Although I fail to see the point in doing this. All you're doing is
trading the use of the % operator for a function call.

Cheers,
Chris
 
A

Andrii V. Mishkovskyi

I'm trying emulate a printf() c statement that does, for example

char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);

In other words, printf() has a variable arguement list the we
all know.

I'm trying to do this in Python...

class MyPrintf(object):
   # blah, blah
    def myprintf(object, *arg):
         # Here I'll have to know I NEED 2 arguments in format string
arg[0]
         print arg[0] % (arg[1], arg[2])

Note: you can, of course, use any name for the instance variable in
methods, but 'self' is considered a de-facto standard, not 'object'.
Besides, you're overriding builtin which is considered a bad practice.
name="Chris"
age=30
printf=MyPrintf()
printf.myPrintf(("My name is %s and I am %d years old.", name, age)
will of course print...
My name is Chris and I am 42 years old.

But
printf.myPrintf(("My name is %s.", name)
of course gives....
Index error: list index out of range

How can I generalize the print call in the myprintf() function to do this?

print arg[0] % (arg[1])
print arg[0] % (arg[1], arg[2])
print arg[0] % (arg[1], ..., arg[n])

It's quite simple:
def printf(fmt, *args):
print fmt % args
 
J

John Machin

I'm trying emulate a printf() c statement that does, for example

char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);

In other words, printf() has a variable arguement list the we
all know.

I'm trying to do this in Python...

| >>> def printf(fmt, *args):
| ... import sys
| ... sys.stdout.write(fmt % args)
| ...
| >>> printf("It's a %s %s parrot\n", 'Norwegian', 'blue')
| It's a Norwegian blue parrot
| >>>

HTH,
John
 
J

John Machin

I'm trying emulate a printf() c statement that does, for example
char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);
In other words, printf() has a variable arguement list the we
all know.
I'm trying to do this in Python...
class MyPrintf(object):
   # blah, blah
    def myprintf(object, *arg):
         # Here I'll have to know I NEED 2 arguments in format string
arg[0]
         print arg[0] % (arg[1], arg[2])
name="Chris"
age=30
printf=MyPrintf()
printf.myPrintf(("My name is %s and I am %d years old.", name, age)
will of course print...
My name is Chris and I am 42 years old.
But
printf.myPrintf(("My name is %s.", name)
of course gives....
Index error: list index out of range
How can I generalize the print call in the myprintf() function to do this?
print arg[0] % (arg[1])
print arg[0] % (arg[1], arg[2])
print arg[0] % (arg[1], ..., arg[n])

def printf(format, *args):
    print format % args

The OP asked for an emulation of printf(), which doesn't have
softspacing and other party tricks.
Although I fail to see the point in doing this. All you're doing is
trading the use of the % operator for a function call.

It is one of those things that it is good to know, perhaps not so good
to practice ... somewhat akin to the English definition of a Scots
gentleman: "A man who can play the bagpipes but doesn't".

Cheers,
John
 
C

Chris Rebert

I'm trying emulate a printf() c statement that does, for example
char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);
In other words, printf() has a variable arguement list the we
all know.
I'm trying to do this in Python...
class MyPrintf(object):
   # blah, blah
    def myprintf(object, *arg):
         # Here I'll have to know I NEED 2 arguments in format string
arg[0]
         print arg[0] % (arg[1], arg[2])
name="Chris"
age=30
printf=MyPrintf()
printf.myPrintf(("My name is %s and I am %d years old.", name, age)
will of course print...
My name is Chris and I am 42 years old.
def printf(format, *args):
    print format % args

The OP asked for an emulation of printf(), which doesn't have
softspacing and other party tricks.

Well, his implementation had the same issues, so I assumed he was
going for something merely somewhat printf()-like rather than exactly
the same. Though I suppose you might as well go all the way if you're
doing something impractical like this. :)

Cheers,
Chris
 
M

Mr. Z

I'm trying emulate a printf() c statement that does, for example

char* name="Chris";
int age=30;
printf("My name is %s", name);
printf("My name is %s and I am %d years old.", %s, %d);

In other words, printf() has a variable arguement list the we
all know.

I'm trying to do this in Python...

class MyPrintf(object):
# blah, blah
def myprintf(object, *arg):
# Here I'll have to know I NEED 2 arguments in format string
arg[0]
print arg[0] % (arg[1], arg[2])

Note: you can, of course, use any name for the instance variable in
methods, but 'self' is considered a de-facto standard, not 'object'.
Besides, you're overriding builtin which is considered a bad practice.
name="Chris"
age=30
printf=MyPrintf()
printf.myPrintf(("My name is %s and I am %d years old.", name, age)
will of course print...
My name is Chris and I am 42 years old.

But
printf.myPrintf(("My name is %s.", name)
of course gives....
Index error: list index out of range

How can I generalize the print call in the myprintf() function to do this?

print arg[0] % (arg[1])
print arg[0] % (arg[1], arg[2])
print arg[0] % (arg[1], ..., arg[n])

It's quite simple:
def printf(fmt, *args):
print fmt % args

Too simple, I love it!
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top