Python instance variables as optional arguments?

Try this: def foo(self, blah=None): if blah is None: # faster than blah == None - thanks to kcwu blah = self. Instance_var.

Perfect, thanks! – Paul Woolcock May 15 '09 at 5:47 3 use "is" to compare None is better (and faster) – kcwu May 15 '09 at 5:49 @kcwu thanks for the suggestion. I knew they were different, but I did a benchmark and found a large performance difference.

– Unknown May 15 '09 at 5:59 @kcwu hmm this is very interesting, using "is" to compare strings also shows it is 50% faster. However, this does not work with integers. – Unknown May 15 '09 at 6:07 1 @Unknown - no not always.

Python interns some strings depending on how they are created etc, but not all. Try ('%s%s' %('x','y')) is ('%s%s' % ('x','y')) instead, or reading two identical strings from a file. – Brian May 15 '09 at 7:45.

No, because the instance doesn't exist when class function definition time You have to rewrite as following def function(self, arg1=val1, arg2=val2, arg3=None): if arg3 is None: arg3 = self. Instance_var This is slightly different to original one: you cannot pass arg3 with None value if you really want. Alternative solution: def function(self, arg1=val1, arg2=val2, **argd): arg3 = argd.

Get('arg3', self. Instance_var).

All the responses suggesting None are correct; if you want to make sure a caller can pass None as a regular argument, use a special sentinel and test with is: class Foo(object): __default = object() def foo(self, blah=Foo. __default): if blah is Foo. __default: blah = self.

Instavar Each call to object() makes a unique object, such that is will never succeed between it and any other value. The two underscores in __default mean "strongly private", meaning that callers know they shouldn't try to mess with it (and would need quite some work to do so, explicitly imitating the name mangling that the compiler is doing). The reason you can't just use the code you posted, btw, is that default values evaluate when the def statement evaluates, not later at call time; and at the time def evaluates, there is as yet no self from which to take the instance variable.

Def foo(self, blah=None): blah = blah if not blah is None else self. Instance_var This works with python 2.5 and forwards and handles the cases where blah is empty strings, lists and so on.

An alternative way of doing this would be: def foo(self, blah=None): blah = blah or self. Instance_var This shorter version looks better, specially when there is more than one optional argument. Use with care.

See the comments below...

Be careful if blah == 0 – kcwu May 15 '09 at 5:54 or False or an empty array… short: don't do it – Georg Schölly May 15 '09 at 6:07 as other commenters point out, this isn't really Pythonic, since it has unexpected effects. – Gregg Lind May 22 '09 at 22:34 You are all right about this. I still think it is helpful,for instance, with strings.

– Sergio May 23 '09 at 1:18.

Request data in environment variables and the standard input stream. It on the standard output stream. CGI protocol (such as webapp).

Variables and the format of the input stream data. The content of message to the standard output stream. An incoming HTTP request includes the HTTP headers sent by the client.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions