강의

멘토링

커뮤니티

Inflearn Community Q&A

dennyi942597's profile image
dennyi942597

asked

Getting Started with Programming: Introduction to Python (Inflearn Original)

functions(1-2)

*args 관련 질문입니다.

Written on

·

157

0

안녕하세요. *args 관련하여 질문 드립니다.

아래와 같이 제가 직접 코드를 짜봤습니다.

test_func를 호출할 때 parameter를 list나 tuple type의 변수를 넣었을 때 아래와 같이 index가 1개밖에 없는것으로 나오는데요....수업시간에 했던 것 처럼,  test_func('Lee', 'Park', 'Kim') 이런식으로 풀어서 parameter 값을 넣어야 하는건가요?

<<<코드>>>

def test_func(*args):

    for i, v in enumerate(args):

        print('index :', i, '\nvalue :', v)

    print('function 실행 완료')

l1 = ['haneol', 'taekon', 'Yoon']

t1 = ('Apple', 'Pear', 'Strawberry')

test_func(t1)

<<<실행결과>>>

index : 0 
value : ('Apple', 'Pear', 'Strawberry')
function 실행 완료
python

Answer 1

0

niceman님의 프로필 이미지
niceman
Instructor

안녕하세요.

팩킹으로 묶어서 매개변수로 보내셨으면 수업 내용대로 

언팩킹 처리를 해줘야 합니다.

def test_func(*args):

for i, v in enumerate(*args):

print('index :', i, '\nvalue :', v)

print('function 실행 완료')

l1 = ['haneol', 'taekon', 'Yoon']

t1 = ('Apple', 'Pear', 'Strawberry')

test_func(t1)
결과
index : 0 value : Apple index : 1 value : Pear index : 2 value : Strawberry function 실행 완료
dennyi942597's profile image
dennyi942597

asked

Ask a question