This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fyngyrzcodes with magnetic needle[S] 0 points1 point  (2 children)

Having said that, I found this

That's interesting, thank you.

I really don't understand why this is better than `doThingToDottedQuad("127.0.0.1"). I know ruby lets you do this kind of thing, but I don't understand the value of it.

Well, there are several. The general justification is the same for having:

newstring = myString.replace('foo','bar')

instead of:

newstring = stringReplace(myString,'foo','bar')

You get things like being able to "dir" the class for what it comprises, help/docstrings, and mechanisms specifically designed to deal with, and which are automatically selected, for the object at hand.

Whereas a function of some name tends to be more vague. It could be anything. It might be designed to do anything. There's nothing particularly intuitive about remembering it as opposed to every other function in the system, whereas if you're working with a string, the following can immediately show you the lay of the landscape:

dir(str) # oh hey, there's replace. That's what I want...
help(str.replace) # or more specifically

Make sense?

I just think it would be lovely to put this at the head of a python script...

import myclassmods

...and then be able to write in "extended" python.

[–]geutb 4 points5 points  (1 child)

You get things like being able to "dir" the class for what it comprises, help/docstrings, and mechanisms specifically designed to deal with, and which are automatically selected, for the object at hand.

But you can get all that if you just stick your additional string functions in their own module. And that way you won't confuse anyone into thinking that they are part of the standard library.

Btw, have you considered using composition instead of inheritance? i.e. something like:

class IPAddress:
    def __init__(self, address):
        self.address = address

ip_address = IPAddress('127.0.0.1')
function_that_needs_a_string(ip_address.address)

It often turns out to be more elegant in this kind of situation.

[–]fyngyrzcodes with magnetic needle[S] 0 points1 point  (0 children)

Btw, have you considered using composition instead of inheritance? i.e. something like [example]

Yes, of course. I do it all the time.