No method overloading

H

Hussein B

Hey,
Please correct me if I'm wrong but Python doesn't support method
overload, right?
 
M

MeTheGameMakingGuy

Hey,
Please correct me if I'm wrong but Python doesn't support method
overload, right?
--
def method(self):
 #code
def method(self, data):
 #code

Correct. The second declaration overwrites the first. Give data a
default argument instead:
def method(self, data=None):
if data is None:
# Normal code
else:
# data included code

Not sure how Pythonic that is, but it should work the way you're
requesting... I think.
 
F

Fredrik Lundh

Hussein said:
Please correct me if I'm wrong but Python doesn't support method
overload, right?
--
def method(self):
#code
def method(self, data):
#code

in Python, methods are callable attributes, and an attribute can only
have one value. you can use default arguments to work around this:

def method(self, data=None):
if data is None:
#code
else:
#code

</F>
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top