Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; 'patterns.': 0.07; 'strict': 0.07; 'variables': 0.07; 'attributes': 0.09; 'namespace': 0.09; 'overwrite': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '"global"': 0.16; 'cleaner': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'globals': 0.16; 'object()': 0.16; 'objects.': 0.16; 'pep8': 0.16; 'prefixed': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'example': 0.22; 'programming': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'module,': 0.24; 'looks': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:g 30': 0.30; 'flying': 0.31; 'object.': 0.31; 'though.': 0.31; 'front': 0.32; 'maybe': 0.34; 'could': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'really': 0.36; 'keyword': 0.36; 'module.': 0.36; 'surely': 0.36; 'doing': 0.36; 'should': 0.36; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; "you're": 0.61; 'you.': 0.62; "you've": 0.63; 'kind': 0.63; 'organization': 0.63; 'refer': 0.63; 'telling': 0.64; 'more': 0.64; 'experience.': 0.67; 'everywhere.': 0.84; 'adopt': 0.91 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Tue, 02 Dec 2014 20:13:25 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131118 Thunderbird/17.0.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python handles globals badly. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417576416 news.xs4all.nl 2874 [2001:888:2000:d::a6]:38859 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81827 On 12/02/2014 07:27 PM, Skybuck Flying wrote: > Excuse is: "bad programming style". > > I don't need snot telling me how to program after 20 years of programming > experience. > > This is so far the only thing pissing me off in python. > > Now I have to declare "global" in front of these variables every where I > want to use em: Only if you want to overwrite them, though. Python has support for any number of programming paradigms. That many global variables is screaming for some kind of organization and Python has good mechanisms for doing this in a much neater way. Might I suggest you use a namespace instead? Put all your "globals" in a module, and refer to them via the module. For example (converting to pep8 which looks better to me): import globals as g def some_func(): g.ship_ability_distribute_shield_power = 5 A whole lot cleaner than trying to use the global keyword everywhere. If you've really been programming for 20 years, surely you're familiar with programming patterns. The fact that you have 6 variables all prefixed with "ShipAbility" should suggest something to you. Like maybe all of these attributes could be encapsulated in one "ship" object. Python is very flexible in how you use objects. You can program in strict java-style if you want, or adopt a more freeform approach: ship = object() ship.ability = object() ship.ability.distribute_shield_power = 5 etc.