Computer Science Canada

__python_tricks__

Author:  wtd [ Sun Jul 16, 2006 4:21 pm ]
Post subject:  __python_tricks__

In Python, you can add all kinds of interesting behavior to your classes by defining functions with names like __call__.

code:
>>> class A(object):
...    def __call__(self):
...       print 42
...
>>> a = A()
>>> a()
42
>>>


How many of these do you know of?

Author:  Null [ Sun Jul 16, 2006 5:41 pm ]
Post subject: 

Very well known, but there is also __str__()

code:

class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return "%s is %s years old" % (self.name, self.age)

person = Person("Joe", 263)
print person

Author:  wtd [ Sun Jul 16, 2006 6:07 pm ]
Post subject: 

Null wrote:
Very well known, but there is also __str__()


I see your __str__ and raise you a __repr__.

code:
>>> class A(object):
...    def __repr__(self):
...       return "Hello world"
...
>>> A()
Hello world
>>>

Author:  Null [ Sun Jul 16, 2006 9:36 pm ]
Post subject: 

__iter__() and __len__()

code:

class LinkedList(object):
    class Node(object):
        def __init__(self, data):
            self.data = data
            self.next = None

    def __init__(self, data_list):
        self.first = self.Node(data_list[0])
        self.__size = 0
        self.current = self.first
       
        for data in data_list[2:]:
            self.append(data)

    def last(self):
        current = self.first

        while current.next is not None:
            current = current.next

        return current

    def append(self, data):
        self.__size += 1
       
        self.last().next = self.Node(data)

    def __len__(self):
        return self.__size

    def __iter__(self):
        return self

    def next(self):
        try:
            if not self.current:
                raise StopIteration

            result = self.current.data
            self.current = self.current.next
            return result

        except StopIteration:
            self.current = self.first
            raise StopIteration

if __name__ == "__main__":
    linked_list = LinkedList(range(1, 11))

    for node in linked_list:
        print node


I love Ruby, I really do. Its consistency, blocks, and pure design. My problem with it is that obscure-ness (is that a word?) in code seems to be preferred, and it starts to look extremely cryptic. I'm starting to lean towards Python, especially when it comes to readability. I'm still deciding which I prefer.

Author:  rizzix [ Mon Jul 17, 2006 4:29 pm ]
Post subject: 

One of the reasons I dont like python is because of these special __methods__. What an ugly naming convention.

Author:  Cervantes [ Mon Jul 17, 2006 4:32 pm ]
Post subject: 

They don't look so bad. They are effective at drawing your attention to them. And so your attention should be drawn to them, as they are special.

Author:  rizzix [ Mon Jul 17, 2006 4:36 pm ]
Post subject: 

I would hardly call __str__, __len__, __iter__, etc, special.

Instead of __init__ they could have simply defined a method called self. Since "self" is used to refer to "this" object, having a method my the name self can safely act as a constructor for "this" object.

(basically lots of things can be done a lot better without the ugly convention)

Author:  McKenzie [ Wed Jul 19, 2006 10:18 am ]
Post subject: 

So you like using compareTo in Java?
Try __lt__(self, other) in Python. Called when < is used.
I like this because I miss the good old operator overloading from C.

Author:  wtd [ Wed Jul 19, 2006 10:51 am ]
Post subject: 

McKenzie wrote:
I like this because I miss the good old operator overloading from C.


I hope you mean C++. Smile

Author:  McKenzie [ Wed Jul 19, 2006 10:36 pm ]
Post subject: 

Embarassed, I actually typed C on purpose. I didn't think about it that much. I recalled learning about operator overloading about 12-14 years ago. I didn't do much C++ back then, so I assumed this was C knowledge.

Author:  wtd [ Thu Jul 20, 2006 9:15 pm ]
Post subject: 

No problem. The compatibilities between the two have made it really hard to push the idea that they're distinct languages.


: