4 σχόλια »

    • #1
    • Giorgos Keramidas
    • It’s amusing to see how Python and the other “modern” languages are moving closer to Lisp as time passes.

      What you describe with the ‘decorators’ is something that has been available as a very very useful side-effect of ‘generic functions” in Lisp.

      For example, here’s how one would define a person class with (name, surname) as its only attributes in Lisp:

      CL-USER> (defclass person ()
                 ((name    :initarg :name
                           :initform nil
                           :accessor name)
                  (surname :initarg :surname
                           :initform nil
                           :accessor surname)))
      => #<standard -CLASS PERSON></standard>

      Now the functions (name person-object) and (surname person-object) work fine, because of the ‘accessor’ methods defined in the class, so you can write:

      CL-USER> (let ((giorgos (make-instance 'person
                                   :name "Giorgos" :surname "Keramidas")))
                 (list (name giorgos)
                       (surname giorgos)))
      => ("Giorgos" "Keramidas")

      This creates an instance of the person class, and calls the two accessor methods to display the existing attributes.

      To build an arbitrary combination of the attributes, you can either concatenate the results right there, or define a new generic function which abstracts away the implementation details of the method.

      Note that the person class is defined by now, but I can type directly in the same Lisp session the following:

      CL-USER> (defgeneric fullname (person))
      => #<standard -GENERIC-FUNCTION FULLNAME (1)></standard>

      This defines a new generic function that can work on person objects, but doesn’t really implement anything yet. You can think of this as a virtual class method in other OO languages.

      Now, let’s implement the actual function:

      CL-USER> (defmethod fullname ((p person))
                 (format nil "~A ~A" (name p) (surname p)))
      => #<standard -METHOD FULLNAME (PERSON) {5A1428C1}></standard>

      That’s it. We now have a new method that knows how to access the appropriate attributes of a person object, do whatever it takes to construct the full name of the person, and return it as a string:

      CL-USER> (let ((giorgos (make-instance 'person
                                :name "Giorgos" :surname "Keramidas")))
                 (fullname giorgos))
      => "Giorgos Keramidas"

      The beauty and power of this is that the special method for doing all this was defined after the original class was set in stone. No changes are needed to the original class, because the model of the generic functions in Lisp allows us to extend the functionality of a class in runtime, long after the original class was developed.

      Now, that’s what I call cool :-)

    • #2
    • Luca Foppiano
    • Interesting :-)

    • #3
    • Toshio Kuratomi
    • Funny thing, I’ve been using properties a lot this week. But I’ve had to use the non-decorator syntax since I’m using them for setters as well as getters. Basically, I wanted to keep a variable settable by the user loosely synced with a copy saved in the filesystem or in a web application. To do this, I had a getter to get the value from the cache and a setter to put it in whenever the element was referenced. Minus error handling, it boiled down to this:

      def _get_session(self):
          if not self._session:
              f = file(SES_FILE, 'r')
              self._session = f.read()
              f.close()
          return self._session
       
      def _set_session(self, session):
          self._session = session
          f = file(SES_FILE, 'w')
          f.write(self._session)
          f.close()
       
      session = property(_get_session, _set_session)

      python 2.6+ should be able to handle this with a decorator as well.

    • #4
    • Kushal Das
    • Thanks for the information :)


Σχολιάστε!




Νιώστε ελεύθερα να πείτε τη γνώμη σας!

Παρακαλώ διαβάστε την πολιτική σχολίων πριν σχολιάσετε.

Η email διεύθυνση σας δεν θα δημοσιοποιηθεί ποτέ. Το πεδίο υπάρχει απλά για να σας στείλω κάποια απάντηση αν θελήσω. Να 'στε σίγουροι, απεχθάνομαι κι εγώ το spam.

XHTML ετικέτες που επιτρέπονται: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . Για τις παραθέσεις χρησιμοποιήστε <blockquote> HTML ετικέτες.

 


Reduce textarea size Increase textarea size


 
π
Τελευταία καταχώρηση: August 17 2008, 20:44 PDT