473,785 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default method arguments

Hello everybody!
I have little problem:

class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use 'A' class as following :

myA = A(5)
myA.f()

and get printed '5' as a result.)

Nov 22 '05 #1
44 2771
On 15 Nov 2005 08:03:26 -0800, gr************* **@gmail.com
<gr************ ***@gmail.com> wrote:
Hello everybody!
I have little problem:

class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use 'A' class as following :

myA = A(5)
myA.f()

and get printed '5' as a result.)


class A:
def __init__(self, n):
self.data = n

def f(self, x=None):
if not x:
x = self.data
print x
myA = A(5)
myA.f()

5

Peace
Bill Mill
bill.mill at gmail.com
Nov 22 '05 #2
> I have little problem:

class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use 'A' class as following :

myA = A(5)
myA.f()

and get printed '5' as a result.)


# use new-style classes, if there's no cogent reason to do otherwise
class A(object):
def __init__(self, n):
self.data = n
def f(self, x = None)
# do NOT use "if not x" !
if x is None:
print self.data
else:
print x

--
Nicola Larosa - ni*******@m-tekNico.net

....Linux security has been better than many rivals. However, even
the best systems today are totally inadequate. Saying Linux is
more secure than Windows isn't really addressing the bigger issue
- neither is good enough. -- Alan Cox, September 2005
Nov 22 '05 #3
> def f(self, x=None):
if not x:
Ha! You fell for it! ;-D
(Hint: what about x being passed with a value of zero? :-) )
x = self.data
print x


--
Nicola Larosa - ni*******@m-tekNico.net

....Linux security has been better than many rivals. However, even
the best systems today are totally inadequate. Saying Linux is
more secure than Windows isn't really addressing the bigger issue
- neither is good enough. -- Alan Cox, September 2005
Nov 22 '05 #4
Nicola Larosa wrote:
# use new-style classes, if there's no cogent reason to do otherwise
class A(object):
def __init__(self, n):
self.data = n
def f(self, x = None)
# do NOT use "if not x" !
if x is None:
print self.data
else:
print x


Using None might be problematic if None could be a valid argument. The
safest way all round is to use a unique object created just for this
purpose:

_marker = object()

class A(object):

def __init__(self, n):
self.data = n

def f(self, x=_marker)
if x is _marker:
x = self.data
print x
Nov 22 '05 #5
Thanks a lot, but that's not what I do really want.
1) f() may have many arguments, not one
2) I don't whant only to _print_ x. I want to do many work with it, so
if I could simply write

def f(self, x = self.data) (*)

it would be much better.

By the way, using

class A(object):
data = 0
....
def f(self, x = data)

solves this problem, but not nice at all

So I think (*) is the best variant, but it doesn't work :(

Nov 22 '05 #6
I want to find REALLY NICE AND PYTHONIC solution (if any)

Nov 22 '05 #7
On 11/15/05, Nicola Larosa <ni*******@m-teknico.net> wrote:
def f(self, x=None):
if not x:


Ha! You fell for it! ;-D
(Hint: what about x being passed with a value of zero? :-) )


I wasn't sure if you saw my post before you posted - good call. I just
tossed off an answer without thinking much, and we see the result. It
could have been a good debugging lesson for him if he'd tried to pass
0; I think I'll use that as my excuse.

Peace
Bill Mill
bill.mill at gmail.com
Nov 22 '05 #8
> Using None might be problematic if None could be a valid argument.

That's like saying that NULL could be a significant value in SQL. In
Python, "None" *is* the empty, not significant value, and should always be
used as such. Specifically, never exchange "None" for "False".

--
Nicola Larosa - ni*******@m-tekNico.net

....Linux security has been better than many rivals. However, even
the best systems today are totally inadequate. Saying Linux is
more secure than Windows isn't really addressing the bigger issue
- neither is good enough. -- Alan Cox, September 2005
Nov 22 '05 #9
gr************* **@gmail.com wrote:
Hello everybody!
I have little problem:

class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use 'A' class as following :

myA = A(5)
myA.f()

and get printed '5' as a result.)


class A(object): # Stop using old-style classes, please
def __init__(self, n):
self.data = n
def f(self, x = None):
if x is None:
x = self.data
print x

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Nov 22 '05 #10

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

Similar topics

14
5277
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while for keyword arguments the tutorial shows: def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
12
12710
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
7
548
by: steve | last post by:
Hi all If I use this code <% If Request.Item("col")="firstcoll" Then Response.Write "Checked"%> in .aspx page for example like that <input type="radio" name="col" value="firstcoll" <% If Request.Item("col")="firstcoll" Then Response.Write "Checked"%>>
3
8758
by: steve | last post by:
Sorry if I have post this two times. Hi all What I'm trying to achieve is: I have a form with two radio buttons, if a visitors check radio button 2 and click submit in the next page radio button 2 to be check by default If I use this code
18
3004
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a constructor that all parameters have default values
8
3046
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 this, if the code is jitted the "push xyz" instructions of the actual default values can be inserted. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself why one to...
14
3275
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 make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
7
9299
by: prefersgolfing | last post by:
In 6, by default arguments, were passed byref what is the default in .NET? Can you point me to any MSDN documentation? TIA
0
2009
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ((string)(this.GetPropertyValue("Address1")));
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.