Hai Vu
Hai Vu

Reputation: 40763

What are the Difference between cElementtree and ElementTree?

I know a little of dom, and would like to learn about ElementTree. Python 2.6 has a somewhat older implementation of ElementTree, but still usable. However, it looks like it comes with two different classes: xml.etree.ElementTree and xml.etree.cElementTree. Would someone please be so kind to enlighten me with their differences? Thank you.

Upvotes: 31

Views: 19395

Answers (5)

Kyrylo Malakhov
Kyrylo Malakhov

Reputation: 1446

From https://docs.python.org/3/library/xml.etree.elementtree.html:

Changed in version 3.3: This module will use a fast implementation whenever available. The xml.etree.cElementTree module is deprecated.

So for Python 3.3 and higher just use:

import xml.etree.ElementTree as ET

Upvotes: 21

logan
logan

Reputation: 357

But now they are the same thing as of Python 3.3, in github source code cElementTree

# cElementTree.py

from xml.etree.ElementTree import *

it is just for backward compatibility

Upvotes: 22

jcdyer
jcdyer

Reputation: 19165

ElementTree is implemented in python while cElementTree is implemented in C. Thus cElementTree will be faster, but also not available where you don't have access to C, such as in Jython or IronPython or on Google App Engine.

Functionally, they should be equivalent.

Upvotes: 5

Desintegr
Desintegr

Reputation: 7090

It is the same library (same API, same features) but ElementTree is implemented in Python and cElementTree is implemented in C.

If you can, use the C implementation because it is optimized for fast parsing and low memory use, and is 15-20 times faster than the Python implementation.

Use the Python version if you are in a limited environment (C library loading not allowed).

Upvotes: 32

Benjamin Wohlwend
Benjamin Wohlwend

Reputation: 31858

From http://effbot.org/zone/celementtree.htm:

The cElementTree module is a C implementation of the ElementTree API, optimized for fast parsing and low memory use. On typical documents, cElementTree is 15-20 times faster than the Python version of ElementTree, and uses 2-5 times less memory

Upvotes: 7

Related Questions