MCPcopy Index your code
hub / github.com/python/cpython / intern

Method intern

Lib/profiling/sampling/string_table.py:10–28  ·  view source on GitHub ↗

Intern a string and return its index. Args: string: The string to intern Returns: int: The index of the string in the table

(self, string)

Source from the content-addressed store, hash-verified

8 self._string_to_index = {}
9
10 def intern(self, string):
11 """Intern a string and return its index.
12
13 Args:
14 string: The string to intern
15
16 Returns:
17 int: The index of the string in the table
18 """
19 if not isinstance(string, str):
20 string = str(string)
21
22 if string in self._string_to_index:
23 return self._string_to_index[string]
24
25 index = len(self._strings)
26 self._strings.append(string)
27 self._string_to_index[string] = index
28 return index
29
30 def get_string(self, index):
31 """Get a string by its index.

Callers 10

namedtupleFunction · 0.80
convert_childrenMethod · 0.80
isinternedFunction · 0.80
test_internMethod · 0.80
InterningTestCaseClass · 0.80
testInternMethod · 0.80
testNoInternMethod · 0.80

Calls 2

strFunction · 0.85
appendMethod · 0.45

Tested by 6

isinternedFunction · 0.64
test_internMethod · 0.64
testInternMethod · 0.64
testNoInternMethod · 0.64