본문 바로가기

Skill up/Programming

[python] 함수 오버로딩(overloading)?

python에서 클래스를 만들고 오버로딩(overloading)을 해볼려고 했더니.. 응?

'Duplicated signature' 라고 뜬다. (현재 python 2.7 + eclipse 사용)

오버로딩이 안되나보다 

방법은?

오버로딩은 아니지만, 함수의 argument를 다양한 타입으로 여러개를 받을 수 있도록 할 수 있다.

class test:
def function(self, *arglist):
print arglist
print arglist[0]

사용할 때는

function('a',1.0, (1,2)) 

그냥 함수 호출하면 된다. 

arglist[0] = 'a'
arglist[1] = 1.0
arglist[2] = (1,2) 

로 들어가 있을 것이다.

좀더 advanced하게 사용하려면..

 
def function(self, *arglist):
        if not arglist:
            print 'no arguments'
            return
        else:
            for i,arg in enumerate(arglist):
                if type(arg)==list:
                    print arg
                elif type(arg)==str:
                    print arg

argument를 하나씩 불어와서 type에 따라 다르게 처리해야 할 경우에 저렇게 사용하면 된다.