using attributes as defaults

W

Wanderer

I want to give the option of changing attributes in a method or using
the current values of the attributes as the default.

class MyClass():
""" my Class
"""
def __init__(self):
""" initialize
"""
self.a = 3
self.b = 4

def MyMethod(self, a = self.a, b = self.b)
""" My Method
"""
self.a = a
self.b = b
DoSomething(a, b)

The above doesn't work. Is there a way to make it work?

Thanks
 
S

Stephen Hansen

I want to give the option of changing attributes in a method or using
the current values of the attributes as the default.

class MyClass():
""" my Class
"""
def __init__(self):
""" initialize
"""
self.a = 3
self.b = 4

def MyMethod(self, a = self.a, b = self.b)
""" My Method
"""
self.a = a
self.b = b
DoSomething(a, b)

The above doesn't work. Is there a way to make it work?

The normal way to go about it would be to set the a/b arguments to None,
like so:

def MyMethod(self, a = None, b = None):
if a is not None:
self.a = a
if b is not None:
self.b = b

DoSomething(self.a, self.b)

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJNTGzBAAoJEKcbwptVWx/lGewH/2t4MfxVgiTc3aK7IiyI1hmK
P0uxEOcigWTGLHa1E1Yb7DVybsEzmJuXQS50OFGcqg3wRo+QVPNVx0DMtpLg5m0l
Eu119nqxHRkfR/BkKih/gklEt5h8UxcIkhyuYLDkZH0AIpHpnmqLvvmeL51G1Zu8
SFaELVexe57uxonlShysBWwabguJXAn+lDgDVszU5GWGocJ/GmarX3/HIWwqi6fC
Tj8fghtK/gMnHXkNtwp20Ssmd6CPDGOEOMGN18nPron0GFc0dLGVDIvxZahFGkah
WgUvXcPIsPQiJS/JX6K6yskmEkpkyvEYu4YPF9BGsgBcbVKzxLFV0gQJwaWrIeM=
=y97z
-----END PGP SIGNATURE-----
 
P

Peter Otten

Wanderer said:
I want to give the option of changing attributes in a method or using
the current values of the attributes as the default.

class MyClass():
""" my Class
"""
def __init__(self):
""" initialize
"""
self.a = 3
self.b = 4

def MyMethod(self, a = self.a, b = self.b)
""" My Method
"""
self.a = a
self.b = b
DoSomething(a, b)

The above doesn't work. Is there a way to make it work?

No. The defaults are evaluated once, when the method is created, and self.a
is only known after the instance has been created.

The Python idiom to achieve what you want is

def MyMethod(self, a=None):
if a is not None:
self.a = a

If None is a valid value for self.a you can use a custom sentinel instead:

missing = object()
class MyClass:
def MyMethod(self, a=missing):
if a is not missing:
self.a = a
 
E

Emile van Sebille

On 2/4/2011 1:08 PM Wanderer said...
I want to give the option of changing attributes in a method or using
the current values of the attributes as the default.

class MyClass():
""" my Class
"""
def __init__(self):
""" initialize
"""
self.a = 3
self.b = 4

def MyMethod(self, a = self.a, b = self.b)
""" My Method
"""
self.a = a
self.b = b
DoSomething(a, b)

The typical approach would be to use None:

def myMethod(self,a=None,b=None):
if a is None: a=self.a
...

Emile
 
J

Jean-Michel Pichavant

Westley said:
This doesn't work because you can't reference keyword arguments in the
keyword argument array. This will work:
class MyClass:

def __init__(self):
""" initialize

Really? These are the worst docstrings ever.

"""
self.a = 3
self.b = 4

def MyMethod(self, a=None, b=None)
if a is not None:
self.a = a
if b is not None:
self.b = b
DoSomething(a, b)
There is an alternative to this None thing:

Don't use optional arguments. Optional arguments are fine ,but I found
myself avoiding using them is often a better choice.
Quoting the zen of python: "Explicit is better than implicit."
If the reason for using optional arguments is that it'll take you 2 sec
less to write the method call, then it sounds kind of wrong. Any other
reason would be valid I guess.

I personnaly use optional arguments only to keep backward compatibility
when changing a method signature.

JM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top