Why one can't pass 'parent' and 'tag' arguments as kwargs to SubElement factory function of...
up vote
0
down vote
favorite
I'm using Python 3.6.5. What works:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(root, "sub")
what doesn't:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(parent=root, tag="sub")
So the only difference is passing parent
and tag
as keyword arguments (with proper keywords, mind you). Also look at the stack trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: SubElement() takes at least 2 arguments (0 given)
How on Earth there were 0 arguments given?
A look at the source for SubElement
: lines 443:459 hasn't struck me with any revelations. As you can see the two positional parameters of SubElement
are indeed named parent
and tag
.
Are we to have second thoughts about validity of this particular Raymond Hettinger's piece of advice?
python arguments parameter-passing elementtree keyword-argument
add a comment |
up vote
0
down vote
favorite
I'm using Python 3.6.5. What works:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(root, "sub")
what doesn't:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(parent=root, tag="sub")
So the only difference is passing parent
and tag
as keyword arguments (with proper keywords, mind you). Also look at the stack trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: SubElement() takes at least 2 arguments (0 given)
How on Earth there were 0 arguments given?
A look at the source for SubElement
: lines 443:459 hasn't struck me with any revelations. As you can see the two positional parameters of SubElement
are indeed named parent
and tag
.
Are we to have second thoughts about validity of this particular Raymond Hettinger's piece of advice?
python arguments parameter-passing elementtree keyword-argument
What Python version are you using, and if 2.x, which encoding? (Note, that XML always needs unicode).
– guidot
Nov 22 at 14:17
Added the Python version used. Encoding shouldn't be an issue at this stage (we're not dumping to file anything yet)
– z33k
Nov 22 at 14:53
1
In Python 2.7, you can pass 'parent' and 'tag' as keyword arguments.
– mzjn
Nov 23 at 6:58
1
When using lxml instead of ElementTree, there is a slightly different error message (both Python 2 and Python 3): "TypeError: SubElement() takes at least 2 positional arguments (0 given)".
– mzjn
Nov 23 at 6:58
Now that's illuminating. What could be the design reasons for enforcing using only positional arguments on users? Fear about the user making a typo? (Then the arguments would be gobbled up by**extra
)
– z33k
Nov 23 at 9:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using Python 3.6.5. What works:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(root, "sub")
what doesn't:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(parent=root, tag="sub")
So the only difference is passing parent
and tag
as keyword arguments (with proper keywords, mind you). Also look at the stack trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: SubElement() takes at least 2 arguments (0 given)
How on Earth there were 0 arguments given?
A look at the source for SubElement
: lines 443:459 hasn't struck me with any revelations. As you can see the two positional parameters of SubElement
are indeed named parent
and tag
.
Are we to have second thoughts about validity of this particular Raymond Hettinger's piece of advice?
python arguments parameter-passing elementtree keyword-argument
I'm using Python 3.6.5. What works:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(root, "sub")
what doesn't:
from xml.etree.ElementTree import Element, SubElement
root = Element("root")
SubElement(parent=root, tag="sub")
So the only difference is passing parent
and tag
as keyword arguments (with proper keywords, mind you). Also look at the stack trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: SubElement() takes at least 2 arguments (0 given)
How on Earth there were 0 arguments given?
A look at the source for SubElement
: lines 443:459 hasn't struck me with any revelations. As you can see the two positional parameters of SubElement
are indeed named parent
and tag
.
Are we to have second thoughts about validity of this particular Raymond Hettinger's piece of advice?
python arguments parameter-passing elementtree keyword-argument
python arguments parameter-passing elementtree keyword-argument
edited Nov 22 at 14:51
asked Nov 22 at 13:43
z33k
307413
307413
What Python version are you using, and if 2.x, which encoding? (Note, that XML always needs unicode).
– guidot
Nov 22 at 14:17
Added the Python version used. Encoding shouldn't be an issue at this stage (we're not dumping to file anything yet)
– z33k
Nov 22 at 14:53
1
In Python 2.7, you can pass 'parent' and 'tag' as keyword arguments.
– mzjn
Nov 23 at 6:58
1
When using lxml instead of ElementTree, there is a slightly different error message (both Python 2 and Python 3): "TypeError: SubElement() takes at least 2 positional arguments (0 given)".
– mzjn
Nov 23 at 6:58
Now that's illuminating. What could be the design reasons for enforcing using only positional arguments on users? Fear about the user making a typo? (Then the arguments would be gobbled up by**extra
)
– z33k
Nov 23 at 9:30
add a comment |
What Python version are you using, and if 2.x, which encoding? (Note, that XML always needs unicode).
– guidot
Nov 22 at 14:17
Added the Python version used. Encoding shouldn't be an issue at this stage (we're not dumping to file anything yet)
– z33k
Nov 22 at 14:53
1
In Python 2.7, you can pass 'parent' and 'tag' as keyword arguments.
– mzjn
Nov 23 at 6:58
1
When using lxml instead of ElementTree, there is a slightly different error message (both Python 2 and Python 3): "TypeError: SubElement() takes at least 2 positional arguments (0 given)".
– mzjn
Nov 23 at 6:58
Now that's illuminating. What could be the design reasons for enforcing using only positional arguments on users? Fear about the user making a typo? (Then the arguments would be gobbled up by**extra
)
– z33k
Nov 23 at 9:30
What Python version are you using, and if 2.x, which encoding? (Note, that XML always needs unicode).
– guidot
Nov 22 at 14:17
What Python version are you using, and if 2.x, which encoding? (Note, that XML always needs unicode).
– guidot
Nov 22 at 14:17
Added the Python version used. Encoding shouldn't be an issue at this stage (we're not dumping to file anything yet)
– z33k
Nov 22 at 14:53
Added the Python version used. Encoding shouldn't be an issue at this stage (we're not dumping to file anything yet)
– z33k
Nov 22 at 14:53
1
1
In Python 2.7, you can pass 'parent' and 'tag' as keyword arguments.
– mzjn
Nov 23 at 6:58
In Python 2.7, you can pass 'parent' and 'tag' as keyword arguments.
– mzjn
Nov 23 at 6:58
1
1
When using lxml instead of ElementTree, there is a slightly different error message (both Python 2 and Python 3): "TypeError: SubElement() takes at least 2 positional arguments (0 given)".
– mzjn
Nov 23 at 6:58
When using lxml instead of ElementTree, there is a slightly different error message (both Python 2 and Python 3): "TypeError: SubElement() takes at least 2 positional arguments (0 given)".
– mzjn
Nov 23 at 6:58
Now that's illuminating. What could be the design reasons for enforcing using only positional arguments on users? Fear about the user making a typo? (Then the arguments would be gobbled up by
**extra
)– z33k
Nov 23 at 9:30
Now that's illuminating. What could be the design reasons for enforcing using only positional arguments on users? Fear about the user making a typo? (Then the arguments would be gobbled up by
**extra
)– z33k
Nov 23 at 9:30
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432332%2fwhy-one-cant-pass-parent-and-tag-arguments-as-kwargs-to-subelement-factory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432332%2fwhy-one-cant-pass-parent-and-tag-arguments-as-kwargs-to-subelement-factory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What Python version are you using, and if 2.x, which encoding? (Note, that XML always needs unicode).
– guidot
Nov 22 at 14:17
Added the Python version used. Encoding shouldn't be an issue at this stage (we're not dumping to file anything yet)
– z33k
Nov 22 at 14:53
1
In Python 2.7, you can pass 'parent' and 'tag' as keyword arguments.
– mzjn
Nov 23 at 6:58
1
When using lxml instead of ElementTree, there is a slightly different error message (both Python 2 and Python 3): "TypeError: SubElement() takes at least 2 positional arguments (0 given)".
– mzjn
Nov 23 at 6:58
Now that's illuminating. What could be the design reasons for enforcing using only positional arguments on users? Fear about the user making a typo? (Then the arguments would be gobbled up by
**extra
)– z33k
Nov 23 at 9:30