Override methods with same name in Python programming [duplicate]

2024/7/7 6:08:17

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.

https://en.xdnf.cn/q/120550.html

Related Q&A

TypeError: function object is not subscriptable in Python 3.4.3?

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…

Insert a value in date format dd-mm-yyyy in dictionary in python

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 …

Collisions arent registering with Python Turtles

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…

Function should clean data to half the size, instead it enlarges it by an order of magnitude

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…

is it possible to add colors to python output? [duplicate]

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 …

Tkinter Function attached to Button executed immediately [duplicate]

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 …

Error!!! cant concatenate the tuple to non float

stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child =…

Using R to fit data from a csv with a gamma function?

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…

regex multiple string match in a python list

I have a list of strings like this:["ra", "dec", "ra-error", "dec-error", "glat", "glon", "flux", "l", "b"]I ne…

python IndentationError: expected an indented block [closed]

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…