namedtuple

How to cast tuple into namedtuple?

二次信任 提交于 2019-12-03 10:31:31
I'd like to use namedtuples internally, but I want to preserve compatibility with users that feed me a ordinary tuple. from collections import namedtuple tuplePi=(1,3.14,"pi") #Normal tuple Record=namedtuple("MyNamedTuple", ["ID", "Value", "Name"]) namedE=Record(2, 2.79, "e") #Named tuple namedPi=Record(tuplePi) #Error TypeError: __new__() missing 2 required positional arguments: 'Value' and 'Name' tuplePi.__class__=Record TypeError: __class__ assignment: only for heap types You can use the *args call syntax: namedPi = Record(*tuplePi) This passes in each element of the tuplePi sequence as a

Did something about `namedtuple` change in 3.5.1?

允我心安 提交于 2019-12-03 09:23:05
On Python 3.5.0: >>> from collections import namedtuple >>> cluster = namedtuple('Cluster', ['a', 'b']) >>> c = cluster(a=4, b=9) >>> c Cluster(a=4, b=9) >>> vars(c) OrderedDict([('a', 4), ('b', 9)]) On Python 3.5.1: >>> from collections import namedtuple >>> cluster = namedtuple('Cluster', ['a', 'b']) >>> c = cluster(a=4, b=9) >>> c Cluster(a=4, b=9) >>> vars(c) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: vars() argument must have __dict__ attribute Seems like something about namedtuple changed (or maybe it was something about vars() ?). Was this

Python syntax for namedtuple

旧巷老猫 提交于 2019-12-01 17:51:22
问题 I see that the Python syntax for a namedtuple is: Point = namedtuple('Point', ['x', 'y']) Why isn't it simpler like so: Point = namedtuple(['x','y']) Its less verbose, 回答1: namedtuple is a factory, returning a class. Consider only expression: namedtuple(['x','y']) What would be the name of class returned by this expression? 回答2: In general, objects don't know what variables they are assigned to: # Create three variables referring to an OrderedPair class tmp = namedtuple('OrderedPair', ['x','y

Python syntax for namedtuple

房东的猫 提交于 2019-12-01 17:45:28
I see that the Python syntax for a namedtuple is: Point = namedtuple('Point', ['x', 'y']) Why isn't it simpler like so: Point = namedtuple(['x','y']) Its less verbose, namedtuple is a factory, returning a class. Consider only expression: namedtuple(['x','y']) What would be the name of class returned by this expression? In general, objects don't know what variables they are assigned to: # Create three variables referring to an OrderedPair class tmp = namedtuple('OrderedPair', ['x','y']) # create a new class with metadata Point = tmp # assign the class to a variable Coordinate = tmp # assign the

Inheriting from a namedtuple base class - Python

你。 提交于 2019-12-01 14:58:56
This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c class Z(Y): def __init__(self, a, b, c, d): super(Z, self).__init__(a, b, c) self.d = d [out]: >>> Z(1,2,3,4) <__main__.Z object at 0x10fcad950> But if the baseclass is a namedtuple : from collections import namedtuple X = namedtuple('X', 'a b c') class Z(X): def __init__(self, a, b, c, d): super(Z, self).__init__(a, b

Inheriting from a namedtuple base class - Python

随声附和 提交于 2019-12-01 13:46:10
问题 This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c class Z(Y): def __init__(self, a, b, c, d): super(Z, self).__init__(a, b, c) self.d = d [out]: >>> Z(1,2,3,4) <__main__.Z object at 0x10fcad950> But if the baseclass is a namedtuple : from collections import

Inherit namedtuple from a base class in python

走远了吗. 提交于 2019-12-01 11:20:30
Is it possible to produce a namedtuple which inherits from a base class? What I want is that Circle and Rectangle are namedtuple s and are inherited from a common base class ('Shape'): from collections import namedtuple class Shape: def addToScene(self, scene): ... Circle=namedtuple('Circle', 'x y radius') Rectangle=namedtuple('Rectangle', 'x1 y1 x2 y2') How would I do that? You can try this: class Circle(Shape, namedtuple('Circle', 'x y radius')): pass (You should consider adding __slots__ to all your three classes to save memory and for sightly faster lookups.) 来源: https://stackoverflow.com

Creating a namedtuple object using only a subset of arguments passed

六眼飞鱼酱① 提交于 2019-12-01 11:10:02
I am pulling rows from a MySQL database as dictionaries (using SSDictCursor) and doing some processing, using the following approach: from collections import namedtuple class Foo(namedtuple('Foo', ['id', 'name', 'age'])): __slots__ = () def __init__(self, *args): super(Foo, self).__init__(self, *args) # ...some class methods below here class Bar(namedtuple('Bar', ['id', 'address', 'city', 'state']): __slots__ = () def __init__(self, *args): super(Bar, self).__init__(self, *args) # some class methods here... # more classes for distinct processing tasks... To use namedtuple , I have to know

Inherit namedtuple from a base class in python

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 08:33:23
问题 Is it possible to produce a namedtuple which inherits from a base class? What I want is that Circle and Rectangle are namedtuple s and are inherited from a common base class ('Shape'): from collections import namedtuple class Shape: def addToScene(self, scene): ... Circle=namedtuple('Circle', 'x y radius') Rectangle=namedtuple('Rectangle', 'x1 y1 x2 y2') How would I do that? 回答1: You can try this: class Circle(Shape, namedtuple('Circle', 'x y radius')): pass (You should consider adding _

Creating a namedtuple from a list

感情迁移 提交于 2019-12-01 08:03:10
问题 Consider a list variable t In [55]: t Out[55]: ['1.423', '0.046', '98.521', '0.010', '0.000', '0.000', '5814251520.0', '769945600.0', '18775908352.0', '2.45024350208e+11', '8131.903', '168485.073', '0.0', '0.0', '0.022', '372.162', '1123.041', '1448.424'] Now consider a namedtuple 'Point': Point = namedtuple('Point', 'usr sys idl wai hiq siq used buff cach free read writ recv send majpf minpf alloc vmfree') How do we convert the variable t to a Point? The most obvious (to me anyways..)