Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'classes,': 0.05; 'attribute': 0.07; 'c++,': 0.07; 'class,': 0.07; 'purpose.': 0.07; 'exist,': 0.09; 'patterns,': 0.09; 'python': 0.11; '...,': 0.16; 'appreciated!': 0.16; 'bus.': 0.16; 'compatible.': 0.16; 'components.': 0.16; 'exists,': 0.16; 'initializes': 0.16; 'subject:between': 0.16; 'subject:object': 0.16; 'time).': 0.16; 'version).': 0.16; 'wording': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'not,': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'instance,': 0.24; 'java': 0.24; 'regardless': 0.24; '(or': 0.24; 'question': 0.24; 'class.': 0.26; 'pass': 0.26; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'said,': 0.30; 'code': 0.31; 'object.': 0.31; 'question:': 0.31; 'class': 0.32; 'this.': 0.32; 'thanks!': 0.32; 'becomes': 0.33; 'bus': 0.33; 'maybe': 0.34; 'could': 0.34; 'classes': 0.35; 'common': 0.35; 'connection': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'possible': 0.36; 'should': 0.36; 'two': 0.37; 'clear': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:u 10': 0.60; 'devices': 0.61; 'hardware': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'such': 0.63; 'become': 0.64; 'charset:windows-1252': 0.65; 'sample': 0.67; 'hints': 0.68; 'received:74.208': 0.68; 'stated': 0.69; 'obvious': 0.74; 'adc': 0.84; 'bus,': 0.84; 'me;': 0.84; 'subject:Best': 0.91; 'hand,': 0.93; 'imagine': 0.93 Date: Sat, 21 Feb 2015 09:28:24 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Best practice: Sharing object between different objects References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:L177aTAn65xsFK3XP1UxIeUkObJrY5MTCs71JgWtrji 3nZ/DHPTu9qjawMxhAYFa/k5eW1q79VcRKX1b2VDt/ItzI3pFA vBL0LV8ybEY34qjtbUB3tL8f8JXjnUUNyPAk0eC4Ng50te/t1f g+I+NWjfJQAT0N9yMoH08DHNDfjIqPhjAzSSt32RA4ZP4XwH0H Mx3vbpUGj7NdWRxY6b92cv77A1Grx6CKEF7uBUCadma3WtAnGD 5BTFzhnPEUuzorr72ZbYdBpkKTysFBCDbO/Xvey77orR2DiqVz YjM3/jM1ws2ZQOoF/VUnR0T/AFryeI51iEn9YuZDWD6pEO2n+W HC5ELMIxdF3xaEMhld+8= X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424528921 news.xs4all.nl 2880 [2001:888:2000:d::a6]:36433 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5880 X-Received-Body-CRC: 691938619 Xref: csiph.com comp.lang.python:86039 On 02/21/2015 07:15 AM, pfranken85@gmail.com wrote: > Hello! > > I have a best-practice question: Imagine I have several hardware devices that I work with on the same I2C bus and I am using the python smbus module for that purpose. The individual devices are sensors, ADC, DAC components. As I said, I would like to derive the corresponding classes from one common class, let's say I2CDevice, so that they can share the same bus connection (I don't want to do a import smbus, ..., self.bus = smbus.SMBus(1) all the time). Of course, I could just pass the the bus reference to each instance, but I am pretty sure that there must be a nicer way to do this. > > In particular, I imagine the following: It should be possible that I have two classes, ADC_I2C, DAC_I2C which share the same base class. Once I create an instance of ADC_I2C or DAC_I2C it should check whether a bus object exists, if not, it should create one and the other class should be able to use this bus reference as well. Do you get my point? I am pretty sure that such a design pattern should exist, maybe also in the reference of DB connections? Unfortunately I did not find something like this. > I think you should write the code (or at least a skeleton version). I suspect the answer will become obvious to you, once you're not worrying about design patterns, or being java compatible. Regardless of whether you inherit from a common base class, both classes can have an attribute with a "bus object" in it. The question becomes who initializes it, and who decides to reuse the "same one". Your wording doesn't make that the least bit clear to me; maybe your sample code will. Many times when you'd have a common base class in Java or C++, you just have to have a common member or two in Python. On the other hand, once you start implementing, maybe it'll become obvious that there's enough shared code for a common base class. The first question is whether an ADC "is-a" bus, or "has-a" bus. Stated that way it sounds like you should have an attribute in ADC which is a bus object. > Any hints are highly appreciated! > > Thanks! > -- DaveA