See Ash Code logo See Ash Code

The constructor was holding state that belonged to one function call

Ashlynn Antrobus
• • 3 min read

My DOCX image extractor used to remember the paragraph it was working on. It didn’t need to.

The public method already received the paragraph as an argument. It then saved that argument on the object so another method could read it:

def __init__(self):
    self.paragraph: Paragraph = None

def extract_images(self, paragraph: Paragraph) -> List[str]:
    self.paragraph = paragraph
    image_blobs: list = self._extract_image_blobs()
    return self._build_base64_images_list(image_blobs)

That attribute existed to carry an input a little farther down the same operation. After the method returned, the extractor still held the last paragraph, even though the next call would replace it.

In the December 3 refactor, I removed the constructor and passed the paragraph to the helper instead.

One dragon hands a sheet of paper to another dragon, who examines it with a magnifying glass.

Illustration: passing the current input directly to a helper.

The helper can ask for what it needs

The revised entry point is:

def extract_images(self, paragraph: Paragraph) -> List[str]:
    image_blobs: list = self._extract_image_blobs(paragraph)
    return self._build_base64_images_list(image_blobs)

And the helper now takes a paragraph argument:

def _extract_image_blobs(self, paragraph: Paragraph) -> list:

Inside it, paragraph._p supplies the XML to search for image references, and paragraph.part.related_parts supplies the related image parts. Previously, both expressions started with self.paragraph.

The extraction work stays in the same place. The difference is that I can read the helper’s signature and see which paragraph it will use. Calling it no longer depends on first calling another method to assign an attribute.

That also removes an awkward initial state. The constructor annotated the attribute as a Paragraph but assigned None. Before the first extraction, the object didn’t contain the kind of value that annotation described.

Two calls, two inputs

Here is an illustrative use, assuming I already have two DOCX paragraph objects:

extractor = DocxImageExtractor()

first_images = extractor.extract_images(first_paragraph)
second_images = extractor.extract_images(second_paragraph)

Both calls still use the same extractor. Each call supplies the paragraph for that operation, and the helper receives that paragraph directly. The object doesn’t keep the first paragraph while waiting for somebody to ask it to process the second.

This doesn’t establish that the class is safe for every possible concurrent use. I haven’t performed that test here, and there are other objects involved in reading a DOCX file. It removes this particular shared attribute and the call-order dependency around it.

It makes a small test easier to arrange, too. A test of the helper can pass a suitable paragraph as an argument. It doesn’t have to assign a hidden piece of object state first and remember that the assignment is part of the setup.

What should stay on the object?

I don’t want to turn this into a rule that classes shouldn’t have state. A converter can have configuration or collaborators that stay useful across several calls. Putting those on the object can describe how the object works.

This paragraph had a shorter lifetime. It belonged to one request to extract images, and the method already had it in hand.

I started by giving the converter real documents to work on. That made the per-document input easier to see. The fixture post covers that change.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.