IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New classmthod
(my 'e' key has been failing me recently)

Normal Python has classes which possess member functions (methods). Normal methods always take an initial parameter (usually called 'self') which refers to the instance of the class. For example:

class Ball(object):\n    def __init__(self):\n        self.x = 0\n\n    def bounce(self, distance):\n        self.x += distance\n\nbasketball = Ball()\nbasketball.bounce(3)


Inside 'bounce' (when we call it), 'self' refers to the basketball object instance, so you can manipulate it. Sometimes, however, you want a method which doesn't care about the instance, only the class. Often, these are constructors:

class Ball(object):\n    def __init__(self):\n        self.x = 0\n        self.y = 0\n\n    def displaced(cls, x, y):\n        b = cls()\n        b.x = x\n        b.y = y\n        return b\n    displaced = classmethod(displaced)\n\nbasketball = Ball.displaced(3, 4)\nbasketball.bounce(3)


classmethod() takes what would have been a normal member function and makes it a class method. Class methods don't receive an instance as the first argument, but a class instead (classes are objects in Python). You have to pass in the class for the method to work on it. The above is pretty trivial--we could have just written b = Ball() instead of b = cls(), but once you start subclassing, you can take advantage of the generic nature of cls.

Hope that helped <:)
"Despite the seemingly endless necessity for doing
so, it's actually not possible to reverse-engineer intended invariants
from staring at thousands of lines of code (not in C, and not in
Python code either)."

Tim Peters on python-dev
New A more concrete example from Ruby
Take a look at their [link|http://www.rubycentral.com/ref/ref_c_file.html|File] class. Look at the class methods and the instance methods.

The class methods are all operations on the filesystem. The instance methods are all operations that you'd do to an individual file. If you see how they draw the distinction, you'll see why it can make sense to distinguish between them.

(The syntax for defining class methods in Ruby differs from Python, but the spirit is the same.)

Cheers,
Ben
To deny the indirect purchaser, who in this case is the ultimate purchaser, the right to seek relief from unlawful conduct, would essentially remove the word consumer from the Consumer Protection Act
- [link|http://www.techworld.com/opsys/news/index.cfm?NewsID=1246&Page=1&pagePos=20|Nebraska Supreme Court]
New Oh, I underrstand class/object methods
The classmethod() thing was just an example of decorator. Other good examples include foo=synchronized(foo), foo=traced(foo), foo=remote(machine_name, foo) and so on. Any thing that modifies the way function executes.

BTW, FuManChu surreptitiously folded function attributes in there as well. I wonder if python is concistant enough that you can write not only

def foo():
pass


foo.author = "me"


but also

1.owner = "me"

or

"blah".speaker = "me"


:)

--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New You can't do that to builtins yet
But you can usually subclass builtins and get what you want.

PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> "thing".x = 0
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'str' object has no attribute 'x'
>>> a = "thing"
>>> a.x = 0
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'str' object has no attribute 'x'
>>> class Thing(str):
... \tpass
...
>>> a = Thing()
>>> a.x = 0
>>> a.x
0
"Despite the seemingly endless necessity for doing
so, it's actually not possible to reverse-engineer intended invariants
from staring at thousands of lines of code (not in C, and not in
Python code either)."

Tim Peters on python-dev
New You can do that in Ruby
You can't do it exactly like you suggested - since there is no method named owner on the number 1, you cannot call that method. You have to create the method first. This works:
\nclass Fixnum;\n  attr_accessor :owner;\nend\n\n1.owner = "me";\nputs 1.owner;\n

Cheers,
Ben
To deny the indirect purchaser, who in this case is the ultimate purchaser, the right to seek relief from unlawful conduct, would essentially remove the word consumer from the Consumer Protection Act
- [link|http://www.techworld.com/opsys/news/index.cfm?NewsID=1246&Page=1&pagePos=20|Nebraska Supreme Court]
     Python decorator proposal - (FuManChu) - (10)
         as a non python user, whats wrong with the old way? -NT - (boxley) - (1)
             Not really wrong, just annoying - (FuManChu)
         I don't understand the example :( - (Arkadiy) - (5)
             classmthod - (FuManChu) - (4)
                 A more concrete example from Ruby - (ben_tilly) - (3)
                     Oh, I underrstand class/object methods - (Arkadiy) - (2)
                         You can't do that to builtins yet - (FuManChu)
                         You can do that in Ruby - (ben_tilly)
         An update on the proposed change - (ChrisR) - (1)
             Why, yes, he did. :) - (FuManChu)

Carpe per diem.
52 ms