Category Archives: Python

python: overload

http://stackoverflow.com/questions/10202938/how-do-i-use-method-overloading-in-python

Sum:

class A:

    def stackoverflow(self, i='some_default_value'):    
        print 'only method'

ob=A()
ob.stackoverflow(2)
ob.stackoverflow()

You can’t have two methods with the same name in Python — and you don’t need to.

See the Default Argument Values section of the Python tutorial. See “Least Astonishment” in Python: The Mutable Default Argument for a common mistake to avoid.

===============================================================================

In Python, you don’t do things that way. When people do that in languages like Java, they generally want a default value (if they don’t, they generally want a method with a different name). So, in Python, you can have default values.

class A(object):  # Remember the ``object`` bit when working in Python 2.x

    def stackoverflow(self, i=None):
        if i is None:
            print 'first form'
        else:
            print 'second form'

As you can see, you can use this to trigger separate behaviour rather than merely having a default value.

>>> ob = A()
>>> ob.stackoverflow()
first form
>>> ob.stackoverflow(2)
second form

python cProfile lib

offical website:http://docs.python.org/2/library/profile.html

 

Sum:

usage:

import cProfile

cProfile.run(“function(para1,param2)”)

eg:

def publishScan(flConverter, publisher, sensorMeassurment, laserMsg):
       # for cProfile.run() to work, the param in the input string has to be global.

      global g_flConverter
      global g_sensorMeassurment
      g_flConverter=flConverter
      g_sensorMeassurment=sensorMeassurment

      #flframe = flConverter.getlaserScan(sensorMeassurment). the following line will actully run it
      cProfile.run(“flframe=g_flConverter.getlaserScan(g_sensorMeassurment)”)
      laserMsg.ranges = flframe
      pub.publish(laserMsg)

 

 

Note:

you can also list the output in your order. check the offical website.

python package structure

Problem:

in eclipse PyDev, when creating a Python package, there’s  __init__py file, what’s it for?

Explanation:

1. its primary function of indicating that a directory is a module.

2.__init__.py can be an empty file but it is often used to perform setup needed for the package(import things, load things into path.

3.more to sum