Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'assign': 0.07; 'caller': 0.07; 'class,': 0.07; 'function,': 0.07; 'variable,': 0.07; 'variables.': 0.07; 'python': 0.09; 'global,': 0.09; 'globals': 0.09; 'modules.': 0.09; 'question?': 0.09; 'restriction': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'unpacking': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'passing': 0.15; '"from': 0.16; "'at": 0.16; 'attributes,': 0.16; 'expression.': 0.16; 'places.': 0.16; 'subject:class': 0.16; 'subtlety': 0.16; 'syntax,': 0.16; 'syntax.': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'module,': 0.17; 'pointed': 0.17; 'script.': 0.17; 'specify': 0.17; 'variables': 0.17; 'module': 0.19; 'variable': 0.20; 'file.': 0.20; 'all,': 0.21; 'trying': 0.21; 'import': 0.21; 'java': 0.21; 'sends': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'class.': 0.23; 'statement': 0.23; "haven't": 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'so.': 0.24; 'script': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wondering': 0.26; 'am,': 0.27; 'first,': 0.27; 'connects': 0.27; 'object,': 0.27; 'initial': 0.28; 'post': 0.28; 'questions?': 0.29; 'case,': 0.29; 'source': 0.29; 'probably': 0.29; 'class': 0.29; 'this.': 0.29; "i'm": 0.29; 'classes': 0.30; 'usually': 0.30; 'function': 0.30; 'code': 0.31; '(and': 0.32; 'server.': 0.32; 'could': 0.32; 'function.': 0.33; 'like:': 0.33; 'qualify': 0.33; 'symbol': 0.33; 'another': 0.33; 'program,': 0.34; 'self': 0.34; 'done': 0.34; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'anything': 0.36; 'should': 0.36; 'too': 0.36; 'problems': 0.36; 'correctly': 0.37; 'two': 0.37; 'passed': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'mean': 0.38; 'object': 0.38; 'some': 0.38; 'things': 0.38; 'takes': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; "you've": 0.61; 'first': 0.61; "you'll": 0.62; 'back': 0.62; 'different': 0.63; 'information': 0.63; 'become': 0.65; 'header:Reply-To:1': 0.68; 'further,': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'as:': 0.75; 'anywhere.': 0.84; 'different.': 0.84; 'object:': 0.84; 'dealt': 0.91; 'from.': 0.93 Date: Thu, 06 Sep 2012 09:41:33 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: shaun Subject: Re: Setting up a class References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:BKjLnQnnXmCW4+HbGUkM3iZZtTnst0f8uSoZxnp6yty qrf8IMypbqne83okFKLxKeg6qYQ3KPetLjPIH3WZHS4IP27jbl rzoLjoZ+wCF3yGZ1YDoUewNhkUsLQJ3uCvO/u9YhliEcNUye4m UDejUeyxtqdEC7VZtQ1MI0srcZUpSLpg6z5dr7yywmJkTDw8Mn Vku3XzydmB+HjEMcjXIKRZMxI9FmG6trlCAkf7p0ZlV4R4BR9n xx/PYWVtV+TqwwR/ntLtyZ8ESDK+azIcL2k+VlB5mV5uTCMUHB qpxfVHIGBVrI1AG67AGvLWZhqJjmQcAsrvhIXMNsw11wxgUYg= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346938934 news.xs4all.nl 6851 [2001:888:2000:d::a6]:49094 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28592 On 09/06/2012 08:00 AM, shaun wrote: > Hi all, > > So I'm trying to to OO a script which is currently in place on work. It connects to the database and makes multiple strings and sends them to a server. > > But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions? > > 1) Is there anything I should know about passing in variables from another script to the class? Things don't get passed to a class. They get passed to an initializer (and to the constructor, but you've probably never used one of those), or to methods of the class. There's only one script in a given program, the other source files are modules. If code in one module wants to use code or data from another module, there are two general ways to do it. One is to use the "from module1 import global1, func2" syntax. In this case, they effectively become globals of the current file. And the other is to use module1.func2() syntax, where you qualify where to find the function. As others have pointed out to you, "from xxx import *" is very risky, as you make everything global, and the reader can no longer tell where a particular symbol comes from. Further, any name collisions are silently dealt with, on the assumption that you REALLY know what you're doing. > > 2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at the top of the class, I use the information I get from the database to fill it and I send it back) Is there anything I'm not doing right with this. You don't pass variables back to a script. If you mean return a value from a function, then say so. Note that a return statement takes a single object, but that object could very well be a tuple. So it's perfectly reasonable for a function to return as: return first, second And the caller might have done something like: a, b = myfunc() first will go into a,and second will go into b. This subtlety is because the expression first,second is a standard way to specify a tuple of size 2. And tuple unpacking can be done similarly with his, hers = expression. No restriction with size of 2, but you do want the same number of items in both places. There is no need to declare any variable, anywhere. If it needs an initial value, then assign it. Further, things 'at the top of the class' are class attributes, not just variables. How can you ask if there's anything you're not doing right when you post no code with your vague question? > 3)When I want to use a method from a class in another class method it never seems to work for me, I have a feeling this is to do with "self" but im not too sure?? If one class method needs to call another method of the same class, you'll usually want to use the self object: class .... def method1(self): arg1 = 42 self.method2(arg1) def method2(self, value); ...dosomething interesting... Naturally, when it's a method of some other class, or when you need a different instance of the same class, then you'd better have an instance to use with it. someinstance.method2(arg1) -- DaveA