MCPcopy Index your code
hub / github.com/RustPython/RustPython / namedtuple

Function namedtuple

Lib/collections/__init__.py:362–534  ·  view source on GitHub ↗

Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', ['x', 'y']) >>> Point.__doc__ # docstring for the new class 'Point(x, y)' >>> p = Point(11, y=22) # instantiate with positional args or keywords >>> p[0] + p[1]

(typename, field_names, *, rename=False, defaults=None, module=None)

Source from the content-addressed store, hash-verified

360 _tuplegetter = lambda index, doc: property(_itemgetter(index), doc=doc)
361
362def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
363 """Returns a new subclass of tuple with named fields.
364
365 >>> Point = namedtuple('Point', ['x', 'y'])
366 >>> Point.__doc__ # docstring for the new class
367 'Point(x, y)'
368 >>> p = Point(11, y=22) # instantiate with positional args or keywords
369 >>> p[0] + p[1] # indexable like a plain tuple
370 33
371 >>> x, y = p # unpack like a regular tuple
372 >>> x, y
373 (11, 22)
374 >>> p.x + p.y # fields also accessible by name
375 33
376 >>> d = p._asdict() # convert to a dictionary
377 >>> d['x']
378 11
379 >>> Point(**d) # convert from a dictionary
380 Point(x=11, y=22)
381 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
382 Point(x=100, y=22)
383
384 """
385
386 # Validate the field names. At the user's option, either generate an error
387 # message or automatically replace the field name with a valid name.
388 if isinstance(field_names, str):
389 field_names = field_names.replace(',', ' ').split()
390 field_names = list(map(str, field_names))
391 typename = _sys.intern(str(typename))
392
393 if rename:
394 seen = set()
395 for index, name in enumerate(field_names):
396 if (not name.isidentifier()
397 or _iskeyword(name)
398 or name.startswith('_')
399 or name in seen):
400 field_names[index] = f'_{index}'
401 seen.add(name)
402
403 for name in [typename] + field_names:
404 if type(name) is not str:
405 raise TypeError('Type names and field names must be strings')
406 if not name.isidentifier():
407 raise ValueError('Type names and field names must be valid '
408 f'identifiers: {name!r}')
409 if _iskeyword(name):
410 raise ValueError('Type names and field names cannot be a '
411 f'keyword: {name!r}')
412
413 seen = set()
414 for name in field_names:
415 if name.startswith('_') and not rename:
416 raise ValueError('Field names cannot start with an underscore: '
417 f'{name!r}')
418 if name in seen:
419 raise ValueError(f'Encountered duplicate field name: {name!r}')

Callers 15

functools.pyFile · 0.90
pkgutil.pyFile · 0.90
selectors.pyFile · 0.90
ssl.pyFile · 0.90
_ASN1ObjectClass · 0.90
inspect.pyFile · 0.90
threading.pyFile · 0.90
statistics.pyFile · 0.90
TestResultsClass · 0.90
wave.pyFile · 0.90
sched.pyFile · 0.90
util.pyFile · 0.90

Calls 15

isinstanceFunction · 0.85
listClass · 0.85
strFunction · 0.85
setFunction · 0.85
enumerateFunction · 0.85
lenFunction · 0.85
reversedFunction · 0.85
internMethod · 0.80
evalFunction · 0.50
splitMethod · 0.45
replaceMethod · 0.45
isidentifierMethod · 0.45

Tested by 15

test_namedtupleMethod · 0.72
test_factoryMethod · 0.72
test_defaultsMethod · 0.72
test_readonlyMethod · 0.72
test_factory_doc_attrMethod · 0.72
test_field_docMethod · 0.72
test_field_doc_reuseMethod · 0.72
test_field_reprMethod · 0.72
test_name_fixerMethod · 0.72