Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 __python_tricks__
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Null




PostPosted: Sun Jul 16, 2006 5:41 pm   Post subject: (No 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
wtd




PostPosted: Sun Jul 16, 2006 6:07 pm   Post subject: (No 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
>>>
Null




PostPosted: Sun Jul 16, 2006 9:36 pm   Post subject: (No 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.
rizzix




PostPosted: Mon Jul 17, 2006 4:29 pm   Post subject: (No subject)

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




PostPosted: Mon Jul 17, 2006 4:32 pm   Post subject: (No 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.
rizzix




PostPosted: Mon Jul 17, 2006 4:36 pm   Post subject: (No 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)
McKenzie




PostPosted: Wed Jul 19, 2006 10:18 am   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Jul 19, 2006 10:51 am   Post subject: (No subject)

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


I hope you mean C++. Smile
McKenzie




PostPosted: Wed Jul 19, 2006 10:36 pm   Post subject: (No 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.
wtd




PostPosted: Thu Jul 20, 2006 9:15 pm   Post subject: (No subject)

No problem. The compatibilities between the two have made it really hard to push the idea that they're distinct languages.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: