Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Python client - subclassing(correct way to subclass client)
Python client - subclassing [message #1233523] Sun, 19 January 2014 22:29 Go to next message
Roy Barkas is currently offline Roy BarkasFriend
Messages: 1
Registered: January 2014
Junior Member
The example that ships with the Python client does this:
class MyMQTTClass:
    def __init__(self, clientid=None):
        self._mqttc = mqtt.Client(clientid)


Before I saw the example I had developed a module that subclasses the client like:
class MqClient(client.Client):
    def __init__(self, name):
        client.Client.__init__(self, name)


Is there any reason why the subclassing method shown in the repo example is preferable to the one I'm using? Mine seems to work fine at this point.
The major difference is that the repo example creates an internal _mqttc object where my method subclasses the client

The (admittedly minor) advantage of my approach is that I can do:
mqClient = MqClient({'name':'test123', etc etc})
mqClient.subscribe(mytopic, myqos)

as opposed to the repo example requiring:
mqClient = MyClient('test123')
mqClient._mqtcc.subscribe(mytopic, myqos)
Re: Python client - subclassing [message #1234392 is a reply to message #1233523] Tue, 21 January 2014 21:58 Go to previous messageGo to next message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 16
Registered: March 2013
Junior Member
Hi Roy,

from a purely OO point of view, both approaches have their own characteristics which might suit different circumstances. Roger's example is not a subclass at all really, it's a class which has the mqtt client as an attribute. This means that the methods on the mqtt client are not directly available on MyMQTTClass, whereas on your class MqClient they are.

It depends what you want from your class. Maybe Roger will explain his reasoning...

Ian
Re: Python client - subclassing [message #1234555 is a reply to message #1234392] Wed, 22 January 2014 08:54 Go to previous message
Roger Light is currently offline Roger LightFriend
Messages: 5
Registered: September 2013
Junior Member
Hi Roy,

I think the confusion here is just some unfortunate naming. The sub-class.py name doesn't actually refer to subclassing, it's a duplicate of the sub[scribing-client].py example but using functions within a class instead of standalone. By that measure, the subclassing example would be sub-subclass.py - elegant! Smile

In general, I agree with Ian that it depends what you want with your class. It may make more sense to have the MQTT features hidden in your application, or it may not.

Cheers,

Roger
Previous Topic:Single threaded MQTT 'C' client
Next Topic:How to install Paho ?
Goto Forum:
  


Current Time: Fri Dec 19 00:35:20 GMT 2014

Powered by FUDForum. Page generated in 0.03077 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software