Mr. Z wrote: > 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])
No you don't: in Python, the % operator on strings expects a tuple as second operand, and args is already a tuple. So: print args[0] % arhgs[1:] will do what you want. Some stylistic issues: - You don't have to put everything in a class in Python. Your myprintf looks a lot like a function to me, so you'll probably want to define it as such. - Don't use object as the first parameter to methods! object is the top-level class for the whole class hierarchy, so you certainly don't want to obscure it with a variable. For methods, use the traditional self as first parameter. - In your function myprintf, the format string seems to be a required argument. If it is, you might want to define as such too: def myprintf(format_string, *args): print format_string % args So now, as you can see, redefining printf in Python is not really interesting... HTH anyway. - Eric - -- http://mail.python.org/mailman/listinfo/python-list