473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Default parameter for a method

I wanted to know if there's any way to create a method that takes a
default parameter, and that parameter's default value is the return
value of another method of the same class. For example:

class A:
def __init__(self):
self.x = 1

def meth1(self):
return self.x

def meth2(self, arg=meth1()):
# The default `arg' should would take the return value of
meth1()
print '"arg" is', arg

This obviously doesn't work. I know I could do

....
def meth2(self, arg=None):
if arg is None:
arg = self.meth1()

but I'm looking for a more straightforward way.
Jun 27 '08 #1
6 1445
s0****@gmail.com wrote:
I wanted to know if there's any way to create a method that takes a
default parameter, and that parameter's default value is the return
value of another method of the same class. For example:

class A:
def __init__(self):
self.x = 1

def meth1(self):
return self.x

def meth2(self, arg=meth1()):
# The default `arg' should would take the return value of
meth1()
print '"arg" is', arg

This obviously doesn't work. I know I could do

...
def meth2(self, arg=None):
if arg is None:
arg = self.meth1()

but I'm looking for a more straightforward way.
You can write this as:

def meth2(self, arg=None):
arg = arg or self.meth1()

IMHO - You can't get much more "straightforward" than that.

-Larry
Jun 27 '08 #2

On Wed, 2008-04-16 at 13:47 -0500, Larry Bates wrote:
s0****@gmail.com wrote:
I wanted to know if there's any way to create a method that takes a
default parameter, and that parameter's default value is the return
value of another method of the same class. For example:

class A:
def __init__(self):
self.x = 1

def meth1(self):
return self.x

def meth2(self, arg=meth1()):
# The default `arg' should would take the return value of
meth1()
print '"arg" is', arg

This obviously doesn't work. I know I could do

...
def meth2(self, arg=None):
if arg is None:
arg = self.meth1()

but I'm looking for a more straightforward way.

You can write this as:

def meth2(self, arg=None):
arg = arg or self.meth1()

IMHO - You can't get much more "straightforward" than that.
What if arg is 0 an empty list or anything else that's "False"?

def meth2(self, arg=None):
arg = (arg is not None) or self.meth1()

is what you want.
Regards,
Cliff
Jun 27 '08 #3
Cliff Wells wrote:
>
On Wed, 2008-04-16 at 13:47 -0500, Larry Bates wrote:
>s0****@gmail.com wrote:
I wanted to know if there's any way to create a method that takes a
default parameter, and that parameter's default value is the return
value of another method of the same class. For example:

class A:
def __init__(self):
self.x = 1

def meth1(self):
return self.x

def meth2(self, arg=meth1()):
# The default `arg' should would take the return value of
meth1()
print '"arg" is', arg

This obviously doesn't work. I know I could do

...
def meth2(self, arg=None):
if arg is None:
arg = self.meth1()

but I'm looking for a more straightforward way.

You can write this as:

def meth2(self, arg=None):
arg = arg or self.meth1()

IMHO - You can't get much more "straightforward" than that.

What if arg is 0 an empty list or anything else that's "False"?

def meth2(self, arg=None):
arg = (arg is not None) or self.meth1()

is what you want.
No, it's not:
>>for arg in None, 0, "yadda":
.... print "---", arg, "---"
.... if arg is None: arg = "call method"
.... print "OP:", arg
.... print "Larry:", arg or "call method"
.... print "Cliff:", (arg is not None) or "call method"
....
--- None ---
OP: call method
Larry: call method
Cliff: True
--- 0 ---
OP: 0
Larry: call method
Cliff: True
--- yadda ---
OP: yadda
Larry: yadda
Cliff: True

Peter

Jun 27 '08 #4
s0****@gmail.com wrote:
I wanted to know if there's any way to create a method that takes a
default parameter, and that parameter's default value is the return
value of another method of the same class. For example:
....
>
def meth2(self, arg=meth1()):
Not good. If the default value of an argument is mutable, there
are wierd effects, because the default value is bound once when the
class is created, then shared between all later uses. This is almost
never what was wanted or intended, and it's a common source of subtle
bugs.

In general, default values should be immutable constants only.
There's been talk of fixing this (it's really a design bug in Python),
but for now, it's still broken.

(I just had horrible thoughts about the implications of binding
a closure to a default argument. You don't want to go there.)

John Nagle
Jun 27 '08 #5

"John Nagle" <na***@animats.comwrote in message
news:48***********************@news.sonic.net...
| s0****@gmail.com wrote:
| I wanted to know if there's any way to create a method that takes a
| default parameter, and that parameter's default value is the return
| value of another method of the same class. For example:
| >
| ...
|
| >
| def meth2(self, arg=meth1()):
|
| Not good. If the default value of an argument is mutable, there
| are wierd effects, because the default value is bound once when the
| class is created, then shared between all later uses. This is almost
| never what was wanted or intended, and it's a common source of subtle
| bugs.
|
| In general, default values should be immutable constants only.

Then one would have to restrict default args to immutable builtins.
There is no way to determine (without reading code) whether instances of a
user-defined class are mutable or not.

tjr

Jun 27 '08 #6
On Apr 16, 4:21*pm, John Nagle <na...@animats.comwrote:
In general, default values should be immutable constants only.
This is more restrictive than necessary; it should rather read "In
general, default values should be *treated as* immutable objects
only". It's perfectly fine for a default value to be mutable if the
function doesn't modify it, as in the following example:

def parse(text, stopwords=set(w.strip() for w in
open('stopwords.txt')):
words = [w for w in text.split() if w not in stopwords]
...

Since the set is not modified, there's no harm for being mutable; IOW
it's no different than using a frozenset instead. Similarly for dicts,
lists and other mutable containers, as long as they are treated as
read-only.

George
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
18
by: Clark Nu | last post by:
It seems that when I define a fuction,I can set a default value to some of the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then...
7
by: Vyssokih Max | last post by:
Hello! In C++, I can wrote: void Update(int count = 0) {...} and use it without parameters or with one parameter
7
by: A.M | last post by:
Hi, Do we have default method parameter in C#? Something like this void method1(int i = 12) { .... }
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
4
by: indigator | last post by:
I have an ASP.Net web service class, DataLayer.asmx.cs. I have two constructors for the DataLayer class. One is the default parameter-less one and the second one accepts a string argument. When I...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
1
by: s0suk3 | last post by:
I had posted this before but all the spam whipped it out... I wanted to know if there's any way to create a method that takes a default parameter, and that parameter's default value is the return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.