가져오기 해킹

내가 프로그래밍을 시작했을 때 나는 가장 긴 코드 라인을 가지고 있어도 괜찮았다. 글쎄, 그것이 작동하는 한, 그렇지?

얼마 후, 나는 아무도 내 코드를 해독할 수 없다는 것을 발견했습니다. 내 손글씨와 비슷합니다.

아무도 내 코드를 읽을 수 없다면 어떻게 성장하거나 고용될 수 있을까요?

다른 모든 프로그래머와 마찬가지로 저도 해결책을 찾아야 했습니다. 네, 알겠습니다.

"해킹 가져오기"솔루션입니다.

모듈 가져오기 및 내보내기는 이미 친숙한 개념입니다. 함수를 가져오고 내보내는 방법에 대해 논의하고 싶습니다.

이렇게 긴 코드를 변환한다고 상상해보세요.

def chatWithFriend():
  print("what is your name")
  friend = input()
  print(f' good day {friend}')
  print(f' how are you, {friend}')
  resp1 = input()
  print(f'what is your favorite food,{friend}')
  print('My favourite food is fried rice')
  print('would like to eat my favorite food')
  resp2 = input()
  print(f'what school do you attend, {friend}')
  print('I attend the university of Nsukka, Nigeria')
  resp3 = input()

chatWithFriend()


...이것으로. 그리고 네, 작동합니다.

#message.py
from greet import *

def chatWithFriend():
  print("what is your name")
  friend = input()
  greetFriend(friend)
  resp1 = input()
  foodFriend(friend)
  resp2 = input()
  schoolFriend(friend)
  resp3 = input()


요령은 하나의 큰 기능을 사용하지 않는 것입니다. 항상 매우 지저분해질 것입니다. 그렇다면 함수를 더 작은 함수로 나누지 않는 이유는 무엇입니까?

새 파일을 만들고 이 기능을 입력하고 기존 파일로 가져옵니다.

다음은 이러한 함수를 가져오는 두 가지 방법입니다.
  • 하나의 함수만 가져오기
    하나의 함수만 가져와야 할 때 사용됩니다.

  • #greet.py
    def greetFriend(friend):
      print(f' good day {friend}')
      print(f' how are you, {friend}')
    



    #message.py
    from greet import greetFriend
    
    def chatWithFriend():
      print("what is your name")
      friend = input()
      greetFriend(friend)
    


  • 모든 기능 가져오기
    이렇게 하면 파일의 모든 기능을 기본 파일로 가져옵니다.

  • #greet.py
    def greetFriend(friend):
      print(f' good day {friend}')
      print(f' how are you, {friend}')
    
    def foodFriend(friend):
      print(f'what is your favorite food,{friend}')
      print('My favourite food is fried rice')
      print('would like to eat my favorite food')
    
    def schoolFriend(friend):
      print(f'what school do you attend, {friend})
      print('I attend the university of Nsukka, Nigeria')
    



    #message.py
    from greet import *
    
    def chatWithFriend():
      print("what is your name")
      friend = input()
      greetFriend(friend)
      resp1 = input()
      foodFriend(friend)
      resp2 = input()
      schoolFriend(friend)
      resp3 = input()
    
    


    보너스 팁


  • 여러 함수만 가져오기
    __ all __이라는 특정 변수를 모듈에서 사용하여 가져올 변수를 제한할 수 있습니다.

  • #greet.py
    
    __all__ = ['greetFriend', 'foodFriend']
    
    def greetFriend(friend):
      print(f'Good day {friend}')
      print(f'How are you, {friend}')
    
    def foodFriend(friend):
      print(f'What is your favorite food,{friend}')
      print('My favourite food is fried rice')
      print('Would like to eat my favorite food')
    
    def schoolFriend(friend):
      print(f'What school do you attend, {friend}')
      print('I attend the university of Nsukka, Nigeria')
    



    #message.py
    from greet import *
    
    def chatWithFriend():
      print("what is your name")
      friend = input()
      greetFriend(friend)
      resp1 = input()
      foodFriend(friend)
      resp2 = input()
    
    


    우리 모두는 프로그래머의 사고방식을 공유합니다. "작동한다면 변경하지 마십시오."나도 그랬는데 안해본사람? 절대 아무도 없습니다.

    어떻게 극복 했습니까? 나는 나 자신에게 말해야 했다. 무한정 초보자로 남아 있고 싶지 않았고 코드를 읽을 수 있는 경우에만 진행할 수 있었습니다. 어려웠지만 스스로를 설득할 수 있었습니다.

    그때 내가 할 수 있다면 당신도 할 수 있습니다. 가져오기 및 내보내기 기능을 사용하지 않는 이유는 무엇입니까?

    좋은 웹페이지 즐겨찾기