Defining our own types?

tobiah st at tobiah.org
Thu Aug 17 16:20:09 EDT 2006


> suppose I type:
> ip = 123.45.67.89

This is probably far from what you want,
but I would do something like this:

********************************************
class ip(list):

        def __init__(self, ip):

                bytes = ip.split('.')
                for x in range(4):
                        self.append(bytes[x])

        def __repr__(self):

                return '.'.join(self)


localhost = ip('192.168.1.1')

print localhost
print localhost[2]
*******************************************
192.168.1.1
1

That way at least you can get at the value
in both of the meaningful ways, and you can
probably think of more methods, like applying
netmasks, etc...





More information about the Python-list mailing list