python error: do_this( ) takes 1 positional argument but 15 were given

2020. 3. 18. 09:49·Error code 모음/4. Python errors
반응형

해당 에러는 파이썬에서 나타나는 전달된 인자 갯수와 관련된 에러타입이다

 

함수는 위치 인자를 1개 취하는데, 이 개수보다 많은 인자가 위치 인자로 전달되었거나,

특정하게 지정되지 않은 인자가 전달되지 않았을 때 나타나는 에러이다.

 

이 상황은 주로 2가지 상황에서 나타나는 것 같은데..

 

첫번째는 class 함수에 self 가 들어가지 않았을 때

위 첫번재 에러의 경우에는 아직 코드로 구현을 하지 않아서 . 나중에 참고하기 위해 

링크를 남겨둔다

https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given

 

TypeError: method() takes 1 positional argument but 2 were given

If I have a class... class MyClass: def method(arg): print(arg) ...which I use to create an object... my_object = MyClass() ...on which I call method("foo") like so... >>>

stackoverflow.com

https://programmers.co.kr/learn/questions/1374

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

두번째가 함수에 많은 인자를 쓰지 않았을 경우이다.

두번째에서 에러가 난 경우는 이렇다

import threading

def do_this(what, *args): # 해당 함수는 1개를 인자로 받는데 .. 아래 for 문으로 인해
    whoami(what)          # 많은 인자가 전달되자 에러가 났었다. 
                          # 그래서 2개 이상의 인자를 받은 수 있는 *args 를 사용했다. 

def whoami(what):
    print("Thread {0} saya : {1}".format(threading.current_thread(), what))


if __name__=="__main__":
    whoami("I'm the main program")
    for n in range(4):
        p = threading.Thread(target = do_this, args = ("I'm functions {0}".format(n)))
        p.start()

do_this( ) 함수의 경우에 what 만으로 1개의 인자만 받을 수 있는 상황인데..

아래 for 문으로 인해 1개 이상의 인자가 들어오는 상황이었다..

그래서 do_this() takes 1 positional argument but 15 were given 가 뜬것인데..

이 코드는 class 함수를 만드는 코드가 아니기 때문에 *args 를 써주어 

do_this( ) 함수가 1개 이상의 인자를 받을 수 있도록 설정해주었다.

 

출처 : 직접 캡쳐

살짝 엉성하긴 하지만..구현하고자 하는 멀티 스레딩은 구현할 수 있었다..

 

두번째의 경우에는 하단 링크를 참고하여 해결할 수 있었다.

https://blog.hannal.com/2015/03/keyword-only-arguments_and_annotations_for_python3/

 

Python 3에서 함수의 키워드 인자 강제와 주석문 · Kay on the rails

Python 3에서 함수의 키워드 인자 강제와 주석문 22 Mar 2015 Python 3에 도입된 함수 선언 문법 중 키워드 인자를 강제하는 방법과 주석문(annotation)이 있다. Python의 매력 요소 중 하나가 깔끔하고 명료한 코드라 생각하는데, 이 두 문법은 기호를 남발하는 코드처럼 보여서 좀 불만스럽지만 코드 문맥(context)을 읽는 데엔 참 유익하다. 그나마 $ 기호가 사용되는 건 아니라서 다행이랄까?! :) 위치 인자 개수 지정 P

blog.hannal.com

 

반응형

'Error code 모음 > 4. Python errors' 카테고리의 다른 글

python error : list indices must be integers or slices, not str  (0) 2020.05.23
python error: Could not import the lzma module. Your installed Python is incomplete.  (0) 2020.04.08
python error: The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.  (0) 2020.03.17
python error L “TypeError: __str__ returned non-string” but still prints to output?  (0) 2020.03.07
python error : __init__() missing 1 required positional argument:  (0) 2020.03.07
'Error code 모음/4. Python errors' 카테고리의 다른 글
  • python error : list indices must be integers or slices, not str
  • python error: Could not import the lzma module. Your installed Python is incomplete.
  • python error: The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.
  • python error L “TypeError: __str__ returned non-string” but still prints to output?
쟈누
쟈누
Ad astra per aspera
    반응형
  • 쟈누
    쟈누의 기록공간
    쟈누
  • 전체
    오늘
    어제
    • 분류 전체보기 (444)
      • AWS (31)
        • Glue (4)
        • S3 (1)
      • 클라우드 (0)
      • Data Engineering (37)
        • GitHub (10)
        • NiFi (11)
        • Spark (10)
        • Snowflake (0)
        • 머신러닝, AI (6)
      • 언어 (118)
        • 데이터 베이스 (42)
        • JAVA (9)
        • Python (34)
        • Java Script (15)
        • Linux (18)
      • 프로젝트, 인강 그리고 책 (30)
        • Spotify Project (7)
        • RASA chatbot Project (9)
        • Naver shopping Project (6)
        • 빅데이터를 지탱하는 기술 (8)
      • OLD (56)
        • IT 용어 사전 (13)
        • Front End (12)
        • Back End (31)
      • Error code 모음 (165)
        • 1. SQL errors (17)
        • 2. Hadoop errors (20)
        • 3. Linux Errors (14)
        • 4. Python errors (33)
        • 5. JAVA, Spring errors (41)
        • 6. Jav Script errors (10)
        • 7. Dev Tools errors (9)
        • 8. Git errors (8)
        • 9. Jenkins Errors (4)
        • 10. airflow Errors (2)
        • 11. Aws errors (7)
      • 개인 (1)
        • 책 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 블로그 관리
    • 글쓰기
  • 링크

  • 공지사항

    • 간단한 블로그 소개
  • 인기 글

  • 태그

    Python
    python error
    json
    자바
    에러
    파이썬
    error
    AWS
    node
    linux
    install
    Git
    Spring
    MySQL
    SQL
    API
    NiFi
    java
    설치
    리눅스
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
쟈누
python error: do_this( ) takes 1 positional argument but 15 were given
상단으로

티스토리툴바