Skip to content

Commit 42549c8

Browse files
committed
Add with statement to example in README.md
Not using with expression in code might cause lack of cleanup and hard to trace errors, also because Bus doesn't override __del__. So it's a good idea to provide a 100% percent correct example in README for people who don't go into examples folder and read into the code.
1 parent 2d6e996 commit 42549c8

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

README.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ Example usage
9191
9292
# create a bus instance
9393
# many other interfaces are supported as well (see documentation)
94-
bus = can.Bus(interface='socketcan',
94+
with can.Bus(interface='socketcan',
9595
channel='vcan0',
96-
receive_own_messages=True)
96+
receive_own_messages=True) as bus:
9797
98-
# send a message
99-
message = can.Message(arbitration_id=123, is_extended_id=True,
100-
data=[0x11, 0x22, 0x33])
101-
bus.send(message, timeout=0.2)
98+
# send a message
99+
message = can.Message(arbitration_id=123, is_extended_id=True,
100+
data=[0x11, 0x22, 0x33])
101+
bus.send(message, timeout=0.2)
102102
103-
# iterate over received messages
104-
for msg in bus:
105-
print(f"{msg.arbitration_id:X}: {msg.data}")
103+
# iterate over received messages
104+
for msg in bus:
105+
print(f"{msg.arbitration_id:X}: {msg.data}")
106106
107-
# or use an asynchronous notifier
108-
notifier = can.Notifier(bus, [can.Logger("recorded.log"), can.Printer()])
107+
# or use an asynchronous notifier
108+
notifier = can.Notifier(bus, [can.Logger("recorded.log"), can.Printer()])
109109
110110
You can find more information in the documentation, online at
111111
`python-can.readthedocs.org <https://python-can.readthedocs.org/en/stable/>`__.

0 commit comments

Comments
 (0)