IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.61k stars 490 forks source link

Why withDelegate returns the Peripheral object? #298

Closed PrzemoF closed 6 years ago

PrzemoF commented 6 years ago

From the docs:

withDelegate(delegate) This stores a reference to a “delegate” object, which is called when asynchronous events such as Bluetooth notifications occur. This should be a subclass of the DefaultDelegate class. See Working with notifications for more information. This method returns the Peripheral object.

I find this a bit confusing. It returns the same object as the constructor. Feel free to close if there is a good reason behind it.

IanHarvey commented 6 years ago

It's a variation of the "builder pattern". Returning self allows you to construct an object and set some properties for it using a convenient single-line construction like:

scanner = Scanner().withDelegate(ScanDelegate())

(from scanner.py).