Override methods with same name in Python programming [duplicate]
2024/11/24 9:35:19
Possible Duplicate:
How do I use method overloading in Python?
I am new to Python programming, and I like to write multiple methods with the same name, but why only the method which is called recently get printed?
Code is below:
class A:def mymethod(self):print 'first method'def mymethod(self):print 'second method'
ob = A()
ob.mymethod()
With the output as second method.
What is the mechanism behind this Python method calling? Can I call two methods of the same name at the same time?
Answer
Discussed here:
Python function overloading
In Python, functions are looked up by name. The types of the arguments are not part of the name, and are not declared anywhere. The function can be called with arguments of any type.
If you write your function using "duck typing" you can usually make one function do all the different jobs you need it to do. Arguments with default values are also often used, to allow for calling the function with a different number of arguments.
Here is a simple example:
class A(object):def __init__(self, value=0.0):self.value = float(value)a = A() # a.value == 0.0
b = A(2) # b.value == 2.0
c = A('3') # c.value = 3.0
d = A(None) # raises TypeError because None does not convert to float
In this example, we want a float value. But we don't test the type of the argument; we just coerce it to float, and if it works, we are happy. If the type is wrong, Python will raise an exception for us.
I have a food menu and the stock and prices are in separate dictionaries.Food Stock:Food_Stock = {Chips : 15,Bagels : 27,Cookies : 25}#Food Stock.Food Prices:Food_Prices = {#Food Prices.Chips : 1,Bagel…
I am creating a dictionary having some values including a date of birth of a person. But when I run my code, it is giving an error "datetime.datetime has no attribute datetime" . Here is my …
Im creating a simple 2D shooter following an online tutorial, and the enemy sprites (except 1, there are 5 total) are not abiding by my collision code. Everything works except the bullet object that th…
This has been driving me nuts all week weekend. I am trying merge data for different assets around a common timestamp. Each assets data is a value in dictionary. The data of interest is stored in lists…
This question already has answers here:How do I print colored text to the terminal?(66 answers)Closed 10 years ago.so i made a small password strength tester for me, my friends and my family, as seen …
This question already has answers here:How can I pass arguments to Tkinter buttons callback command?(2 answers)Closed 8 years ago.What I need is to attach a function to a button that is called with a …
Using the following data: time_stamp,secs,count
2013-04-30 23:58:55,1367366335,32
2013-04-30 23:58:56,1367366336,281
2013-04-30 23:58:57,1367366337,664
2013-04-30 23:58:58,1367366338,1255
2013-04-30…
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…