Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36538

Re: Class confusion

References <CABRP1o9HuUR3e5S5wj8FvNgqAW6jnqb7iBJ0yo9J+H1fNWfAxQ@mail.gmail.com> <50EDE103.6080609@mrabarnett.plus.com> <CANuMAv_gJijvKy8kSYWkTVCNJBhkReoXsd06AjzFOCP0wDCw2g@mail.gmail.com> <CABRP1o-t1umCj8=JYooejU-CY8MSktJaB5=EJha3fMPoSj+-NA@mail.gmail.com>
From Rodrick Brown <rodrick.brown@gmail.com>
Date 2013-01-09 19:43 -0500
Subject Re: Class confusion
Newsgroups comp.lang.python
Message-ID <mailman.350.1357778962.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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 <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):
>>    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 <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 = 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>
>>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Class confusion Rodrick Brown <rodrick.brown@gmail.com> - 2013-01-09 19:43 -0500

csiph-web