Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

객체의 메소드에 관한 질문드립니다! #5

Closed
MyeongJin-Kim opened this issue Jul 24, 2020 · 1 comment
Closed

객체의 메소드에 관한 질문드립니다! #5

MyeongJin-Kim opened this issue Jul 24, 2020 · 1 comment

Comments

@MyeongJin-Kim
Copy link

안녕하세요! 1번 2번 문제를 풀면서 궁금한 점이 생겨 질문드립니다.

1번 문제를 풀다가 str 타입의 객체는 .lower() 같은 메소드를 사용했을 때 원본이 변하지 않는 것을 확인했고
2번 문제를 풀다가 list 타입의 객체에서는 .reverse()를 사용했을 때 원본이 변하는 것을 확인했습니다.

이렇게 객체의 메소드를 호출했을 때 원본이 변하냐 변하지 않느냐는 mutable / immutable 차이 때문에 발생하는 건가요?

image

@likejazz
Copy link
Collaborator

likejazz commented Jul 24, 2020

안녕하세요.

mutable/immutable 차이에 따른 메소드 작성 가이드가 PEP에 정의되어 있는지는 확인이 필요할 것 같습니다. 다만, 기본적으로는 말씀하신대로 str은 immutable이므로 in-place 수정이 불가능합니다. 값을 return 하는 것 외에는 변경된 값을 전달할 수 있는 방법이 없습니다. 물론 이렇게 되면 추가적인 공간 복잡도가 필요하게 되므로 가능하기만 하다면 리스트 처럼 in-place 수정이 가장 좋겠습니다.

리턴 여부는 함수 작성자가 결정할 문제이므로 이 방식이 PEP에 정의되어 있는 공식 가이드 인지는 확인이 필요할 것 같습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants