Reputation: 361
I am trying to use collections.namedtuple
. I went through the documentation and encountered following syntaxes.
Person = namedtuple('Person', 'name,age,gender')
anup = Person(name='Anup', age=21, gender='male')
Following also works fine
Person = namedtuple('Person', 'name age gender')
anup = Person(name='Anup', age=21, gender='male')
I came across following syntax which was something like this.
Person = namedtuple('Person', 'name age gender' 'address phone')
Ref:https://github.com/tensorflow/models/blob/master/textsum/batch_reader.py#L29
If I try to do the following
anup = Person(name='Anup', age=21, gender='male', address='xyz', phone='1234')
it throws an error stating
TypeError: __new__() got an unexpected keyword argument 'gender'
but this seems to be working absolutely fine.
anup = Person(name='Anup', age=21, genderaddress='xyz', phone='1234')
I am unable to understand the syntax, and how the two attributes have merged together.
Upvotes: 1
Views: 319
Reputation: 96257
Because this:
Person = namedtuple('Person', 'name age gender' 'address phone')
Is equivalent to this:
Person = namedtuple('Person', 'name age genderaddress phone')
Here is the documentation:
Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus,
"hello" 'world'
is equivalent to"helloworld"
.
So note, this is just part of the python syntax, and nothing special about namedtuple
.
In the link, we have the following:
ModelInput = namedtuple('ModelInput',
'enc_input dec_input target enc_len dec_len '
'origin_article origin_abstract') ^
Notice the space.
Upvotes: 3