Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'one?': 0.05; 'parameter': 0.05; 'dictionary': 0.07; 'python': 0.08; 'e.g.': 0.13; 'wrote:': 0.14; 'def': 0.14; 'abstract': 0.16; 'dictionary,': 0.16; 'ideally,': 0.16; 'object?': 0.16; 'received:192.168.200': 0.16; 'cc:no real name:2**0': 0.17; 'objects': 0.17; 'values': 0.18; 'attributes': 0.18; 'cc:2**0': 0.21; 'code,': 0.21; 'object': 0.22; 'vs.': 0.23; 'header:In-Reply-To:1': 0.23; 'cc:addr:python- list': 0.24; 'martin': 0.24; 'parameters': 0.25; 'code': 0.25; 'classes': 0.25; "haven't": 0.26; 'define': 0.27; 'structure': 0.27; 'perhaps': 0.29; 'class': 0.29; 'example': 0.29; 'subject:?': 0.29; 'thanks': 0.30; 'example.': 0.30; 'however': 0.30; 'cc:addr:python.org': 0.30; 'none': 0.32; 'however,': 0.32; 'file': 0.34; 'accessing': 0.34; 'like:': 0.34; 'received:192': 0.34; 'there': 0.34; 'pass': 0.34; 'header:User-Agent:1': 0.34; 'hi,': 0.34; 'using': 0.35; 'skip:_ 10': 0.36; 'rather': 0.36; 'received:192.168': 0.37; 'think': 0.37; "i'd": 0.38; 'clear': 0.38; 'skip:f 20': 0.38; 'too': 0.39; 'subject:: ': 0.39; 'throughout': 0.39; 'skip:a 20': 0.40; 'your': 0.61; 'forced': 0.67; '100': 0.70; 'trick,': 0.84 X-IronPort-AV: E=Sophos;i="4.60,457,1291590000"; d="scan'208";a="991616" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Fri, 11 Feb 2011 21:01:48 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: Martin De Kauwe Subject: Re: Class or Dictionary? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 61 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1297454530 news.xs4all.nl 34849 [::ffff:82.94.164.166]:53705 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55512 Martin De Kauwe wrote: > Hi, > > I have a series of parameter values which i need to pass throughout my > code (>100), in C I would use a structure for example. However in > python it is not clear to me if it would be better to use a dictionary > or build a class object? Personally I think accessing the values is > neater (visually) with an object rather than a dictionary, e.g. > > x = params['price_of_cats'] * params['price_of_elephants'] > > vs. > > x = params.price_of_cats * params.price_of_elephants > > So currently I am building a series of class objects to hold different > parameters and then passing these through my code, e.g. > > class EmptyObject: > pass > > self.animal_prices = EmptyObject() > self.price_of_cats = 12 or reading a file and populating the object > > > I would be keen to hear any reasons why this is a bad approach (if it > is, I haven't managed to work this out)? Or perhaps there is a better > one? > > thanks > > Martin > Using classes is the best choice. However, if because there would be too much classes to define so that you are forced to use your EmptyObject trick, adding attributes to the inntance dynamically, I'd say that dictionnaries are a more common pattern. Ideally, you would have something like: class PriceHolder(object): @classmethod def fromFile(cls, filename): # example of abstract method pass class Animals(PriceHolder): def __init__(self): self.cat = None self.elephant = None class Fruits(PriceHolder): def __init__(self): self.banana = None self.apple = None Then you would have to write 100 PriceHolder subclasses... JM