split up classmethods in python
Question: I am often using classmethods instead of the default constructor in python for
example:

    
    
    class Data(object):
        def __init__(self, x, y, z):
            self.x = x etc..
    
        @classmethod
        def from_xml(cls, xml_file):
            x, y, z  = import_something_from_xml(xml_file)
            return cls(x,y,z)
    

this approach works good, but since i often have large classmethod-
constructors I want to split them up in smaller functions. My problem with
that is, that these smaller functions can be seen in the Class namespace, Is
there any way to avoid this ?


Answer: You can mark the smaller helper functions as private:

    
    
    @classmethod
    def __import_something_from_xml(cls, data):
        #logic
        return a, b, c
    

and you would run:

    
    
    @classmethod
    def from_xml(cls, xml_file):
        x, y, z  = cls.__import_something_from_xml(xml_file)
        return cls(x,y,z)
    

Keep in mind this is only naming convention and this method can be accessed
from Data namespace.

Or you can designate a helper class:

    
    
    class XMLDataHelper:
        @staticmethod
        def import_something_from_xml(data):
            #logic
            return a, b, c
    

And the code would look like this

    
    
    @classmethod
    def from_xml(cls, xml_file):
        x, y, z  = XMLDataHelper.import_something_from_xml(xml_file)
        return cls(x,y,z)
    

