Skip to content

usb-device-cdc: optmise CDCInterface constructor#1138

Open
dpgeorge wants to merge 2 commits into
micropython:masterfrom
dpgeorge:usb-device-cdc-optimise
Open

usb-device-cdc: optmise CDCInterface constructor#1138
dpgeorge wants to merge 2 commits into
micropython:masterfrom
dpgeorge:usb-device-cdc-optimise

Conversation

@dpgeorge

Copy link
Copy Markdown
Member

Summary

Don't set CDCInterface instance attributes that will just be reset by self.init().

Saves 30 bytes of bytecode.

Also simplify the associated example.

Testing

Built but not yet tested on hardware.

Trade-offs and Alternatives

This makes it less clear what exactly are the instance attributes, because their initialisation is done in .init(), not the constructor. The added comment tries to work around that.

Generative AI

Not used.

dpgeorge added 2 commits July 15, 2026 16:11
Don't set instance attributes that will just be reset by `self.init()`.

Saves 30 bytes of bytecode.

Signed-off-by: Damien George <damien@micropython.org>
Makes the example a little bit simpler.

Signed-off-by: Damien George <damien@micropython.org>
cdc = CDCInterface()
cdc.init(timeout=0) # zero timeout makes this non-blocking, suitable for os.dupterm()
# Zero timeout makes this non-blocking, suitable for os.dupterm().
cdc = CDCInterface(timeout=0)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to make this change, but I thought it was a little cleaner to set the timeout in the constructor.

@dpgeorge dpgeorge requested a review from projectgus July 15, 2026 07:03
@dpgeorge

Copy link
Copy Markdown
Member Author

@projectgus curious what you think about this change? I can close the PR if you don't think it's a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant