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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks