<html>
<body>
At 10:55 AM 9/1/2003 -0300, Batista, Facundo wrote:<br><br>
<blockquote type=cite class=cite cite><font size=2>In others languages I
can do (using sintaxis of Python):</font> <br><br>
<font size=2>def myMethod (self, name):</font> <br>
<font size=2>self.name =
name</font> <br><br>
<font size=2>def myMethod (self, name, age):</font> <br>
<font size=2>self.name =
name</font> <br>
<font size=2>self.age =
age</font> <br><br>
<font size=2>If I'm not wrong, this is "Method
Overloading".</font> <br><br>
<font size=2>I thought using defaults:</font> <br><br>
<font size=2>def myMethod (self, name, age=None):</font> <br>
<font size=2>self.name =
name</font> <br>
<font size=2>if age not
None:</font> <br>
<font size=2>self.age = age</font> <br><br>
<font size=2>but I'm concerned about the scalability of this.</font>
<br><br>
<font size=2>What's the most pythonic way to do this? Using something like *args or **args?</font> </blockquote><br>
Depends. In your example, overloading is resolved by the # of args passed, and their meaning depends on their position. If that is all you want, specify defaults as in age=None. Overloading can also depend on the type of data passed to the method, in which case you'll need either an if .. elif structure or a dictionary keyed by types to resolve things.<br><br>
You can also define different methods with the same name by subclassing and using instances of the various subclasses in your various situations. This approach obviates the need for overloading and can make the intent of the program a lot clearer to the reader.<br><br>
IMHO the latter is a better way to deal with complex situations. I often find myself coding default values and if elif structures, and a bell rings reminding me to sublcass. The result is almost always better.<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>