473,795 Members | 2,839 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
44 2776
Gregory Petrosyan <gr************ ***@gmail.com> wrote:
...
def f(self, x = self.data) (*) ... So I think (*) is the best variant, but it doesn't work :(


It DOES work -- it just does not work the way you _expect_ it to work,
but rather, it works the way it's _defined_ to work.

Specifically: all the evaluation of default arguments' values happens as
a part of the execution of the 'def' statement, and so, in particular,
happens at the TIME 'def' is executing.

A 'def' statement which is at the top level in a 'class' statement
evaluates as part of the evaluation of 'class'.

So, if, *while the 'class' statement is evaluating*, something known as
'self' exists, and has a 'data' attribute, this will give you the
default value for argument 'x'. If the name 'self' is unknown, or
refers to an object which has no 'data' attribute, then of course
appropriate exceptions get raised (NameError or AttributeError) when
Python is TRYING to execute that 'def' statement.

Here's an example in which this works without exceptions:

class outer(object):
def __init__(self, data): self.data = data
def make_inner(self ):
class inner(object):
def f(self, x=self.data):
print x
return inner()

Now, y = outer(23).make_ inner() gives you an instance of an inner class,
such that y.f() is the same thing as y.f(23). The 'self.data' in the
'def f', since it's the evaluation of a default value for an argument,
evaluates at the time 'def' evaluates -- and, at that time, 'self'
refers to the instance of class outer that's the only argument to method
make_inner of class outer.

While such "factories" (classes and functions making and returning other
classes and functions) are rarely used by beginners, they are an
extremely important idiom for advanced users of Python. But the point
is that, by having extremely simple and universal rules, it takes no
exoteric knowledge to understand what the above Python code will do --
default argument values evaluate as 'def' executes, therefore there is
absolutely no ambiguity or difficulty to understand when this
'self.data' in particular evaluates.

If Python tried to guess at when to evaluate default argument values,
sometimes during the 'def', sometimes abstrusely storing "something"
(technically a 'thunk') for potential future evaluation, understanding
what's going on in any given situation would become extremely
complicated. There are many languages which attempt to ``helpfully''
"do what the programmer meant in each single case" rather than follow
simple, clear and universal rules about what happens when; as a
consequence, programmers in such "helpful" languages spend substantial
energy fighting their compilers to try and work around the compilers'
attempted "helpfulnes s".

Which is why I use Python instead. Simplicity is a GREAT virtue!
If it's crucial to you to have some default argument value evaluated at
time X, then, by Python's simple rules, you know that you must arrange
for the 'def' statement itself to execute at time X. In this case, for
example, if being able to have self.data as the default argument value
is the crucial aspect of the program, you must ensure that the 'def'
runs AFTER self.data has the value you desire.

For example:

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

This way, of course, each instance a of class A will have a SEPARATE
callable attribute a.f which is the function you desire; this is
inevitable, since functions store their default argument values as part
of their per-function data. Since you want a.f and b.f to have
different default values for the argument (respectively a.data and
b.data), therefore a.f and b.f just cannot be the SAME function object
-- this is another way to look at your issue, in terms of what's stored
where rather than of what evaluates when, but of course it leads to
exactly the same conclusion.

In practice, the solutions based on None or sentinels that everybody has
been suggesting to you are undoubtedly preferable. However, you can, if
you wish, get as fancy as you desire -- the next level of complication
beyond the simple factory above is to turn f into a custom descriptor
and play similar tricks in the __get__ method of f (after which, one can
start considering custom metaclasses). Exactly because Python's rules
and building blocks are simple, clean, and sharp, you're empowered to
construct as much complication as you like on top of them.

That doesn't mean you SHOULD prefer complication to simplicity, but it
does mean that the decision is up to you.
Alex
Nov 22 '05 #11
Dennis Lee Bieber wrote:

(snip) but that may not be desirable if None is a valid value => myA.f(None),
so...

class A(object):
def __init__(self, n):
self.data =n
def f(self, *arg):
if len(arg) == 0:
x = self.data
else:
x = arg[0]
print x


Another solution to this is the use of a 'marker' object and identity test:

_marker = []
class A(object):
def __init__(self, n):
self.data =n
def f(self, x = _marker):
if x is _marker:
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 #12
Nicola Larosa wrote:
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".

You don't think there is a difference in SQL between a field explicitly set
to NULL or a field initialised with a default value?

What you should be saying here is never use "None" when you actually mean
"use the default non-None value for this parameter".
Nov 22 '05 #13
Alex Martelli wrote, in part:
If it's crucial to you to have some default argument value evaluated at
time X, then, by Python's simple rules, you know that you must arrange
for the 'def' statement itself to execute at time X. In this case, for
example, if being able to have self.data as the default argument value
is the crucial aspect of the program, you must ensure that the 'def'
runs AFTER self.data has the value you desire.

For example:

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

This way, of course, each instance a of class A will have a SEPARATE
callable attribute a.f which is the function you desire; this is
inevitable, since functions store their default argument values as part
of their per-function data. Since you want a.f and b.f to have
different default values for the argument (respectively a.data and
b.data), therefore a.f and b.f just cannot be the SAME function object
-- this is another way to look at your issue, in terms of what's stored
where rather than of what evaluates when, but of course it leads to
exactly the same conclusion.


FWIT and ignoring the small typo on the inner def statement (the
missing ':'), the example didn't work as I (and possibily others) might
expect. Namely it doesn't make function f() a bound method of
instances of class A, so calls to it don't receive an automatic 'self''
argument when called on instances of class A.

This is fairly easy to remedy use the standard new module thusly:

import new
class A(object):
def __init__(self, n):
self.data = n
def f(self, x = self.data):
print x
self.f = new.instancemet hod(f, self, A)

This change underscores the fact that each instance of class A gets a
different independent f() method. Despite this nit, I believe I
understand the points Alex makes about the subject (and would agree).

-Martin

Nov 22 '05 #14
Great thanks, Alex!

Nov 22 '05 #15
bruno at modulix wrote:
Another solution to this is the use of a 'marker' object and identity test:
_marker = []
class A(object):
def __init__(self, n):
self.data =n
def f(self, x = _marker):
if x is _marker:
x = self.data
print x


I'll add my 2 cents to the mix:

default = object()

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

def f(self, x=default):
if x is default:
x = self.data
print x
--
Benji York
Nov 22 '05 #16
gr************* **@gmail.com writes:
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 :


Store your default value in a container, and test for it:

class A:
_data = [None]
def __init__(self, n):
self._data = [n]
def f(self, x = _data):
if x is self._data:
x = x[0]
print x

There are lots of variations on this theme.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #17
Benji York <be***@benjiyor k.com> writes:
I'll add my 2 cents to the mix:

default = object()

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

def f(self, x=default):
if x is default:
x = self.data
print x


There were a lot of solutions like this. I'd like to point out that
you can put the "marker" in the class:

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

def f(self, x = default):
if x is self.default:
x = self.data
print x

This way you don't pollute the module namespace with class-specific
names. You pollute the class namespace instead - which seems like an
improvement.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #18
I'm not very familiar with Python, so please explain me why should
containers be used?
For example in one of Paul Graham's essays there's an example of
'generator of accumulators' in Python:

def foo(n):
s = [n]
def bar(i):
s[0] += i
return s[0]
return bar

1) So, why just using 's = n' is not suitable? (It doesn't work, Python
'doesn't see' s, but why?)
2) Is 'foo.s = n' a correct solution? It seems to be a little more
elegant. (I tested it, and it worked well)

Sorry for possibly stupid questions.

Nov 22 '05 #19
"Gregory Petrosyan" <gr************ ***@gmail.com> writes:
I'm not very familiar with Python, so please explain me why should
containers be used?
For example in one of Paul Graham's essays there's an example of
'generator of accumulators' in Python:

def foo(n):
s = [n]
def bar(i):
s[0] += i
return s[0]
return bar

1) So, why just using 's = n' is not suitable? (It doesn't work, Python
'doesn't see' s, but why?)
The Python assignment statements bind a name to a value. By default,
they bind it in the current namespace. Doing "s = n" (or s <op>= n) in
the function bar binds the name s in the function bar, and leaves the
value in foo as it was. "s[0] = i" (or s[0 += i) binds the name s[0],
not the name s, and hence mutates the object bound to s instead of
binding s in the function bar's namespace. In reality, this is
implemented by a mutator method of s, but it *looks* like you're
binding s[0].
2) Is 'foo.s = n' a correct solution? It seems to be a little more
elegant. (I tested it, and it worked well)


It's basically the same solution. You're replacing binding a variable
with mutating an object bound to a name in an outer scope. In one case
the container is named s and is a list that you're setting an element
of. In the other case, the container is named foo and is an object
that you're setting an attribute on.

<miker

--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #20

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

Similar topics

14
5279
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
2010
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
9673
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
9522
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
10216
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
10165
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
10002
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
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
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.