Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.04; 'mrab': 0.05; 'sufficient': 0.05; '__name__': 0.07; 'attributes': 0.07; 'python': 0.09; 'meaningful': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'skip:# 20': 0.13; ':-)': 0.13; 'skip:p 40': 0.15; '"system': 0.16; "'__main__':": 0.16; 'literals': 0.16; 'simple.': 0.16; '\xa0def': 0.16; '\xa0you': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'jan': 0.18; '>>>': 0.18; 'email addr:gmail.com>': 0.20; 'trying': 0.21; 'work,': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; "haven't": 0.23; 'idea': 0.24; 'id:': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'wrote': 0.26; 'extend': 0.26; 'i.e.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.27; 'this?': 0.28; '>>>>': 0.29; 'methods.': 0.29; 'url:mailman': 0.29; '8bit%:5': 0.29; 'skip:_ 10': 0.29; 'skip:& 10': 0.29; 'class': 0.29; "i'm": 0.29; 'code': 0.31; 'url:python': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'anyone': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'thanks': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'method': 0.36; 'test': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'sure': 0.38; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'gave': 0.65; 'brown': 0.65; 'satellite': 0.65; '2013': 0.84; 'sufficient?': 0.84; 'subject:Class': 0.91; 'responses': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=1IEJsLPqkbf5hW/ArZkdBdVlzMMaHdbghWxJ40m71e0=; b=hht+07+42Zjdag98TT81uv6tHOb8l2EUfGQREjgWDKsbr52QVP08zRGC5K+GVoqg/h NKzwNtjzXcgprpMagIPQtcubnPg81QTJ1GEk7CTnWP70w9mJyou/3q905M6npT7B+rpE iVkoItRVlLfAu2Sv9ILF/a89Br5oXgVCx8VOOsOU5UZ6WgmRP3Y6N9ijiwIKlr/bK4Dm hOULLcFXHVSt7XvF2xvlJ1+uadLBH23NpI//LkhRNJxTiqRMVWAiNSbP89/mxGcAyiPQ w3toOKXXbe1duX6xxssamVXAH8euKLhNkqYQ6f6kKQorZ23ZEIN2hfXcSBryqJ0BlNSJ RpJg== MIME-Version: 1.0 In-Reply-To: References: <50EDE103.6080609@mrabarnett.plus.com> From: Matt Jones Date: Wed, 9 Jan 2013 21:19:27 -0600 Subject: Re: Class confusion To: Rodrick Brown Content-Type: multipart/alternative; boundary=bcaec54ee6d4a8113104d2e6a6fb Cc: "python-list@python.org" 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: 315 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357787991 news.xs4all.nl 6932 [2001:888:2000:d::a6]:48087 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36554 --bcaec54ee6d4a8113104d2e6a6fb Content-Type: text/plain; charset=ISO-8859-1 Does this look sufficient for what? You haven't actually told us what it is you're trying to accomplish. I gave you the "how", you must supply the "why". *Matt Jones* On Wed, Jan 9, 2013 at 6:43 PM, Rodrick Brown wrote: > Can anyone care to advise on the following? Based on the responses does > this look sufficient? > > #!/opt/local/bin/python > > class SystemList(object): > sysmap = { '1039' : 'nebula', > '1040' : 'mercury'} > > def __init__(self, sysid): > self.sysid = sysid > > def get_sysname(self): > return self.sysmap[self.sysid] > > class System(object): > def __init__(self): > pass > > def get_hostname(self,sysid): > return SystemList(sysid) > > if __name__ == '__main__': > sc = System() > > for sysid in ('1039','1040'): > print(sc.get_hostname(sysid).get_sysname()) > > > > On Wed, Jan 9, 2013 at 5:18 PM, Rodrick Brown wrote: > >> On Wed, Jan 9, 2013 at 4:34 PM, Matt Jones wrote: >> >>> # Something like... >>> >>> class SystemList(object): >>> def get_systemid(self): >>> return "System Id: bleh" >>> >>> def get_running_kernel(self): >>> return "Kernel: bleh" >>> >>> >>> class SatelliteConnect(object): >>> def get_systemlist(self): >>> return SystemList() >>> >>> >>> # Now the code you wrote would work, only return those literals thought, >>> you'd want to do something meaningful inside of SystemList's methods. >>> >>> >> Thanks for the tip Matt, I had no idea it was so simple. :-) >> >> >>> *Matt Jones* >>> >>> >>> On Wed, Jan 9, 2013 at 3:28 PM, MRAB wrote: >>> >>>> On 2013-01-09 20:13, Rodrick Brown wrote: >>>> >>>>> How can I make a class that has methods with attributes and other >>>>> functions? >>>>> I see a lot of code >>>>> >>>>> >>>>> I'm reading the documentation to Redhat's Satellite software which has >>>>> a >>>>> XMLRPC interface and wrote the following code to test the api. >>>>> >>>>> I would like to extend this code to support methods with methods? I see >>>>> this done a lot in python code but I'm not sure how to accomplish >>>>> something like this? >>>>> >>>>> i.e. >>>>> >>>>> sc = SatelliteConnect() >>>>> sc.get_systemlist().get_**systemid() ? >>>>> or >>>>> sc.get_systemlist().get_**running_kernel() >>>>> >>>>> How does one chain methods and attributes like this with classes? >>>>> >>>>> [snip] >>>> This: >>>> >>>> sc.get_systemlist().get_**systemid() >>>> >>>> simply means that the method "get_systemlist" returns an instance of >>>> some class (let's call it "SystemList") which has a method >>>> "get_systemid". >>>> >>>> -- >>>> http://mail.python.org/**mailman/listinfo/python-list >>>> >>> >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >>> >> > --bcaec54ee6d4a8113104d2e6a6fb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Does this look sufficient for what? =A0You haven't actually told us = what it is you're trying to accomplish. =A0I gave you the "how&quo= t;, you must supply the "why".

Matt Jones


On Wed, Jan 9, 2013 at 6:43 PM, Rodrick = Brown <rodrick.brown@gmail.com> wrote:
Can anyone care to advise on the following? Based on the r= esponses does this look sufficient?

#!/opt/local/bi= n/python

class SystemList(object):
=A0 = =A0 sysmap =3D { '1039' : 'nebula',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'1040' : 'mercury'}

=A0 =A0 def __init__(self, sysid):
=A0 =A0 = =A0 =A0 self.sysid =3D sysid

=A0 =A0 def get_sysna= me(self):
=A0 =A0 =A0 =A0 return self.sysmap[self.sysid]

class System(object):
=A0 =A0 def __init__(se= lf):
=A0 =A0 =A0 =A0 pass

=A0 =A0 def ge= t_hostname(self,sysid):
=A0 =A0 =A0 =A0 return =A0SystemList(sysi= d)

if __name__ =3D=3D '__main__':
=A0 =A0 sc =3D System= ()

=A0 =A0 for sysid in ('1039','1040&= #39;):
=A0 =A0 =A0 =A0 print(sc.get_hostname(sysid).get_sysname()= )



On Wed, Jan 9, 2013 at 5:18 PM, Rodr= ick Brown <rodrick.brown@gmail.com> wrote:
On Wed, Jan 9, 2013 at= 4:34 PM, Matt Jones <matt.walker.jones@gmail.com>= wrote:
# Something like...

class SystemList(object):
=A0 =A0de= f get_systemid(self):
=A0 =A0 =A0 return "System Id: bleh&qu= ot;

=A0 =A0def get_running_kernel(self):
=A0 =A0 =A0 re= turn "Kernel: bleh"


class SatelliteConnect(object):
=A0 =A0def get_systeml= ist(self):
=A0 =A0 = =A0 return SystemList()


# Now the code you wrote would work, only return those literals = thought, you'd want to do something meaningful inside of SystemList'= ;s methods.


<= div>Thanks for the tip Matt, I had no idea it was so simple. :-)=A0
=A0
Matt Jones


On Wed, Jan 9, 2013 at 3:28 PM, MRAB <python@mrabarnett.plus.com> wrote:
On 2013-01-09 20:13, Rodrick Brown wrote:
How can I make a class that has methods with attributes and other
functions?
I see a lot of code


I'm reading the documentation to Redhat's Satellite software which = has a
XMLRPC interface and wrote the following code to test the api.

I would like to extend this code to support methods with methods? I see
this done a lot in python code but I'm not sure how to accomplish
something like this?

i.e.

sc =3D SatelliteConnect()
sc.get_systemlist().get_systemid() ?
or
sc.get_systemlist().get_running_kernel()

How does one chain methods and attributes like this with classes?

[snip]
This:

=A0 =A0 sc.get_systemlist().get_systemid()

simply means that the method "get_systemlist" returns an instance= of
some class (let's call it "SystemList") which has a method "get_systemid".

--
http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list




--bcaec54ee6d4a8113104d2e6a6fb--