473,795 Members | 2,887 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 2774
Thanks a lot, I understood the rule. Let's don't discuss this
(containers etc.) anymore, or it'll be offtopic.

Nov 22 '05 #21
On Tue, 15 Nov 2005 18:44:23 +0100, 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 would like to see _marker put inside the class' scope. That prevents
somebody from the outside scope easily passing _marker as an argument to
instance.f. It also neatly encapsulates everything A needs within A.

class A(object):
_marker = []
def __init__(self, n):
self.data =n
def f(self, x = _marker):
if x is self.__class__. _marker:
# must use "is" and not "=="
x = self.data
print x

Note the gotcha though: in the method definition, you refer to a plain
_marker, but in the method code block, you need to qualify it.
--
Steven.

Nov 22 '05 #22
Steven D'Aprano 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 would like to see _marker put inside the class' scope. That prevents
somebody from the outside scope easily passing _marker as an argument to
instance.f.


if you don't want people to be able to easily pass _marker as an argument
to the f method, you probably shouldn't use it as the default value.

</F>

Nov 22 '05 #23
On Tue, 15 Nov 2005 23:51:18 +0100, "Fredrik Lundh" <fr*****@python ware.com> wrote:
Steven D'Aprano 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 would like to see _marker put inside the class' scope. That prevents
somebody from the outside scope easily passing _marker as an argument to
instance.f.


if you don't want people to be able to easily pass _marker as an argument
to the f method, you probably shouldn't use it as the default value.

LOL ;-)

Regards,
Bengt Richter
Nov 22 '05 #24
On Tue, 15 Nov 2005 23:51:18 +0100, "Fredrik Lundh" <fr*****@python ware.com> wrote:
Steven D'Aprano 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 would like to see _marker put inside the class' scope. That prevents
somebody from the outside scope easily passing _marker as an argument to
instance.f.


if you don't want people to be able to easily pass _marker as an argument
to the f method, you probably shouldn't use it as the default value.

LOL ;-)

Regards,
Bengt Richter
Nov 22 '05 #25
On 15 Nov 2005 11:02:38 -0800, "Martin Miller" <gg************ ****@dfgh.net> wrote:
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).

Or as Alex mentioned, a custom descriptor etc is possible, and can also
protect against replacing f by simple instance attribute assignment
like inst.f = something, or do some filtering to exclude non-function
assignments etc., e.g., (not sure what self.data is really
needed for, but we'll keep it):

BTW, note that self.data initially duplicates the default value,
but self.data per se is not used by the function (until the instance
method is replace by one that does, see further on)
class BindInstMethod( object): ... def __init__(self, inst_fname):
... self.inst_fname = inst_fname
... def __get__(self, inst, cls=None):
... if inst is None: return self
... return inst.__dict__[self.inst_fname].__get__(inst, cls) # return bound instance method
... def __set__(self, inst, val):
... if not callable(val) or not hasattr(val, '__get__'): # screen out some impossible methods
... raise AttributeError, '%s may not be replaced by %r' % (self.inst_fnam e, val)
... inst.__dict__[self.inst_fname] = val
...

The above class defines a custom descriptor that can be instatiated as a class
variable of a given name. When that name is thereafter accessed as an attribute
of an instance of the latter class (e.g. A below), the decriptor __get__ or __set__
methods will be called (the __set__ makes it a "data" descriptor, which intercepts
instance attribute assignment.
class A(object): ... def __init__(self, n):
... self.data = n
... def f(self, x = self.data):
... print x
... self.__dict__['f'] = f # set instance attr w/o triggering descriptor
... f = BindInstMethod( 'f')
...
a = A(5)
a.f <bound method A.f of <__main__.A object at 0x02EF3B0C>>

Note that a.f is dynamically bound at the time of a.f access, not
retrieved as a prebound instance method.
a.f() 5 a.f('not default 5') not default 5 a.data 5 a.data = 'not original data 5'
Since the default is an independent duplicate of a.data
a call with no arg produces the original default: a.f() 5 a.data 'not original data 5' a.f('this arg overrides the default') this arg overrides the default

Try to change a.f a.f = 'sabotage f' Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 10, in __set__
AttributeError: f may not be replaced by 'sabotage f'

Now use a function, which should be accepted (note: a function, not an instance method) a.f = lambda self: self.data*2
a.f <bound method A.<lambda> of <__main__.A object at 0x02EF3B0C>>
Plainly the method was dynamically bound
a.f() 'not original data 5not original data 5'
That was self.data*2 per the lambda we just assigned to a.f
BTW, the assignment is not directly to the instance attribute.
It goes via the descriptor __set__ method.
a.data = 12
a.f() 24 b = A('bee')
b.f <bound method A.f of <__main__.A object at 0x02EF3BAC>> b.f() bee b.f('not bee') not bee b.data 'bee' b.data = 'no longer bee'
b.f() bee b.data 'no longer bee' b.f = lambda self: ' -- '.join([self.data]*3)
b.data 'no longer bee' b.data = 'ha'
b.f() 'ha -- ha -- ha' b.f = lambda self, n='default of n':n
b.data 'ha' b.f(123) 123 b.f() 'default of n' a.f() 24

Now let's add another name that can be used on instances like f A.g = BindInstMethod( 'g')
a.g = lambda self:'a.g'
a.f() 24 a.g() 'a.g' a.g(123) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: <lambda>() takes exactly 1 argument (2 given)
Aha, the bound method got self as a first arg, but we defined g without any args.
b.g Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 7, in __get__
KeyError: 'g'
No instance method b.g defined yet (A.__init__ only defines f)

Make one with a default b.g = lambda self, x='xdefault': x
b.g() 'xdefault' b.g('and arg') 'and arg' a.g() 'a.g'
If we bypass method assignment via the descriptor, we can sapotage it:
a.g = lambda self: 'this works'
a.g() 'this works' a.g = 'sabotage' Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 10, in __set__
AttributeError: g may not be replaced by 'sabotage'
That was rejected

but, a.__dict__['g'] = 'sabotage' # this will bypass the descriptor
a.g Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 7, in __get__
AttributeError: 'str' object has no attribute '__get__'
The descriptor couldn't form a bound method since 'sabotage' was not
a function or otherwise suitable.

But we can look at the instance attribute directly: a.__dict__['g'] 'sabotage'

We could define __delete__ in the descriptor too, but didn't so
del a.g

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: __delete__

We could have made the descriptor return a.g if unable to form a bound method,
but then you'd probably want to permit arbitrary assignment to the descriptor-controlled
attributes too ;-)

Regards,
Bengt Richter
Nov 22 '05 #26
On 15 Nov 2005 11:02:38 -0800, "Martin Miller" <gg************ ****@dfgh.net> wrote:
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).

Or as Alex mentioned, a custom descriptor etc is possible, and can also
protect against replacing f by simple instance attribute assignment
like inst.f = something, or do some filtering to exclude non-function
assignments etc., e.g., (not sure what self.data is really
needed for, but we'll keep it):

BTW, note that self.data initially duplicates the default value,
but self.data per se is not used by the function (until the instance
method is replace by one that does, see further on)
class BindInstMethod( object): ... def __init__(self, inst_fname):
... self.inst_fname = inst_fname
... def __get__(self, inst, cls=None):
... if inst is None: return self
... return inst.__dict__[self.inst_fname].__get__(inst, cls) # return bound instance method
... def __set__(self, inst, val):
... if not callable(val) or not hasattr(val, '__get__'): # screen out some impossible methods
... raise AttributeError, '%s may not be replaced by %r' % (self.inst_fnam e, val)
... inst.__dict__[self.inst_fname] = val
...

The above class defines a custom descriptor that can be instatiated as a class
variable of a given name. When that name is thereafter accessed as an attribute
of an instance of the latter class (e.g. A below), the decriptor __get__ or __set__
methods will be called (the __set__ makes it a "data" descriptor, which intercepts
instance attribute assignment.
class A(object): ... def __init__(self, n):
... self.data = n
... def f(self, x = self.data):
... print x
... self.__dict__['f'] = f # set instance attr w/o triggering descriptor
... f = BindInstMethod( 'f')
...
a = A(5)
a.f <bound method A.f of <__main__.A object at 0x02EF3B0C>>

Note that a.f is dynamically bound at the time of a.f access, not
retrieved as a prebound instance method.
a.f() 5 a.f('not default 5') not default 5 a.data 5 a.data = 'not original data 5'
Since the default is an independent duplicate of a.data
a call with no arg produces the original default: a.f() 5 a.data 'not original data 5' a.f('this arg overrides the default') this arg overrides the default

Try to change a.f a.f = 'sabotage f' Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 10, in __set__
AttributeError: f may not be replaced by 'sabotage f'

Now use a function, which should be accepted (note: a function, not an instance method) a.f = lambda self: self.data*2
a.f <bound method A.<lambda> of <__main__.A object at 0x02EF3B0C>>
Plainly the method was dynamically bound
a.f() 'not original data 5not original data 5'
That was self.data*2 per the lambda we just assigned to a.f
BTW, the assignment is not directly to the instance attribute.
It goes via the descriptor __set__ method.
a.data = 12
a.f() 24 b = A('bee')
b.f <bound method A.f of <__main__.A object at 0x02EF3BAC>> b.f() bee b.f('not bee') not bee b.data 'bee' b.data = 'no longer bee'
b.f() bee b.data 'no longer bee' b.f = lambda self: ' -- '.join([self.data]*3)
b.data 'no longer bee' b.data = 'ha'
b.f() 'ha -- ha -- ha' b.f = lambda self, n='default of n':n
b.data 'ha' b.f(123) 123 b.f() 'default of n' a.f() 24

Now let's add another name that can be used on instances like f A.g = BindInstMethod( 'g')
a.g = lambda self:'a.g'
a.f() 24 a.g() 'a.g' a.g(123) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: <lambda>() takes exactly 1 argument (2 given)
Aha, the bound method got self as a first arg, but we defined g without any args.
b.g Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 7, in __get__
KeyError: 'g'
No instance method b.g defined yet (A.__init__ only defines f)

Make one with a default b.g = lambda self, x='xdefault': x
b.g() 'xdefault' b.g('and arg') 'and arg' a.g() 'a.g'
If we bypass method assignment via the descriptor, we can sapotage it:
a.g = lambda self: 'this works'
a.g() 'this works' a.g = 'sabotage' Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 10, in __set__
AttributeError: g may not be replaced by 'sabotage'
That was rejected

but, a.__dict__['g'] = 'sabotage' # this will bypass the descriptor
a.g Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 7, in __get__
AttributeError: 'str' object has no attribute '__get__'
The descriptor couldn't form a bound method since 'sabotage' was not
a function or otherwise suitable.

But we can look at the instance attribute directly: a.__dict__['g'] 'sabotage'

We could define __delete__ in the descriptor too, but didn't so
del a.g

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: __delete__

We could have made the descriptor return a.g if unable to form a bound method,
but then you'd probably want to permit arbitrary assignment to the descriptor-controlled
attributes too ;-)

Regards,
Bengt Richter
Nov 22 '05 #27
What you want is essentially :

if parm_x is not supplied, use self.val_x

So why not just express it clearly at the very beginning of the
function :

def f(self, parm_x=NotSuppl ied, parm_y=NotSuppl ied ,,,)
if parm_x is NotSupplied: parm_x = self.val_x
if parm_y is NotSupplied: parm_y = self.val_y

Much easier to understand than the "twisting your arm 720 degree in the
back" factory method, IMO.

Gregory Petrosyan wrote:
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 #28
What you want is essentially :

if parm_x is not supplied, use self.val_x

So why not just express it clearly at the very beginning of the
function :

def f(self, parm_x=NotSuppl ied, parm_y=NotSuppl ied ,,,)
if parm_x is NotSupplied: parm_x = self.val_x
if parm_y is NotSupplied: parm_y = self.val_y

Much easier to understand than the "twisting your arm 720 degree in the
back" factory method, IMO.

Gregory Petrosyan wrote:
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 #29
Steven D'Aprano wrote:
I would like to see _marker put inside the class' scope. That prevents
somebody from the outside scope easily passing _marker as an argument
to instance.f. It also neatly encapsulates everything A needs within
A.


Surely that makes it easier for someone outside the scope to pass in
marker:

class A(object):
_marker = []
def __init__(self, n):
self.data =n
def f(self, x = _marker):
if x is self.__class__. _marker:
# must use "is" and not "=="
x = self.data
print x
instance = A(5)
instance.f(inst ance._marker) 5

What you really want is for the marker to exist only in its own little
universe, but the code for that is even messier:

class A(object):
def __init__(self, n):
self.data =n
def make_f():
marker = object()
def f(self, x = _marker):
if x is _marker:
x = self.data
print x
return f
f = make_f()

instance = A(6)
instance.f()

6

Nov 22 '05 #30

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
9519
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
10439
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
10215
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
10001
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...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.