Skip to content
Minsing Log
Go back

English Edition

Hiding Methods or Property Names by Starting Them with _ to Make Them Private

Programming & CS

Situation

def __make_corpus_passages(self, row):
	# Corpus to passages
	passage = Passage(
	id=row['_id'],
	content=row['text'],
	filepath=self.file_path,
	metadata_etc={'title': row['title']}
    )
	return passage

The method above was declared in the parent class, BaseBeirEvaluator. When I applied it to BeirScifactEvaluator, its child class, in the apply method that creates the ground-truth passage in the code below,

gt_passages = gt_passages.apply(self.__make_corpus_passages, axis=1).tolist()

it said that this method did not exist.

Solution

When a method or property name begins with an underscore _, it is designated as a private member. Therefore, these methods or properties cannot be accessed from outside. They can only be used within the same scope. Therefore, it said that the __make_corpus_passages method could not be found because it was private~

def make_corpus_passages(self, row):
	# Corpus to passages
	passage = Passage(
	id=row['_id'],
	content=row['text'],
	filepath=self.file_path,
	metadata_etc={'title': row['title']}
    )
	return passage

By making a method or property private, it prevents the values inside the function from being altered externally.

Originally, I had intended to make make_corpus_passage private to make it impossible to alter.

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
Calling a static method from a method of the same class or a different class
Next Post
How to Redefine an Inherited Method for Each Dataset = Method Override