Re: class or instance method

2009-06-21 Thread Paul Johnston
Hi,

 class class_or_instance(object):
     def __init__(self, fn):
...

This works a treat, thank-you.

Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class or instance method

2009-06-21 Thread Scott David Daniels

Hrvoje Niksic wrote:

...
class class_or_instance(object):
def __init__(self, fn):
self.fn = fn
def __get__(self, obj, cls):
if obj is not None:
return lambda *args, **kwds: self.fn(obj, *args, **kwds)
else:
return lambda *args, **kwds: self.fn(cls, *args, **kwds)
...


Just to polish a bit:

import functools

class ClassOrInstance(object):
def __init__(self, fn):
self._function = fn
self._wrapper = functools.wraps(fn)

def __get__(self, obj, cls):
return self._wrapper(functools.partial(self._function,
   cls if obj is None else obj))


--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: class or instance method

2009-06-21 Thread Miles Kaufmann

On Jun 21, 2009, at 5:23 PM, Scott David Daniels wrote:

Hrvoje Niksic wrote:

...
class class_or_instance(object):
   def __init__(self, fn):
   self.fn = fn
   def __get__(self, obj, cls):
   if obj is not None:
   return lambda *args, **kwds: self.fn(obj, *args, **kwds)
   else:
   return lambda *args, **kwds: self.fn(cls, *args, **kwds)
...


Just to polish a bit:

   import functools

   class ClassOrInstance(object):
   def __init__(self, fn):
   self._function = fn
   self._wrapper = functools.wraps(fn)

   def __get__(self, obj, cls):
   return self._wrapper(functools.partial(self._function,
  cls if obj is None else obj))




from types import MethodType

class ClassOrInstance(object):
def __init__(self, func):
self._func = func

def __get__(self, obj, cls):
return MethodType(self._func, cls if obj is None else obj, cls)


-Miles

--
http://mail.python.org/mailman/listinfo/python-list


class or instance method

2009-06-17 Thread Paul Johnston
Hi,

I would like to have a method that is both a classmethod and an
instancemethod. So:

class MyClass(object):
  @class_or_instance
  def myfunc(cls_or_self):
pass

The semantics I'd like are:
When you call MyClass.myfunc, it gets passed a class
When you call MyClass().myfunc, it gets passed an instance

I'm sure I've seen some code to do this somewhere, but I can't find it
now. Any help appreciated.

Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class or instance method

2009-06-17 Thread Bruno Desthuilliers

Paul Johnston a écrit :

Hi,

I would like to have a method that is both a classmethod and an
instancemethod. So:

class MyClass(object):
  @class_or_instance
  def myfunc(cls_or_self):
pass

The semantics I'd like are:
When you call MyClass.myfunc, it gets passed a class
When you call MyClass().myfunc, it gets passed an instance

I'm sure I've seen some code to do this somewhere, but I can't find it
now. Any help appreciated.


IIRC, there's something quite similar in formencode.

--
http://mail.python.org/mailman/listinfo/python-list


Re: class or instance method

2009-06-17 Thread Xavier Ho
I'm quite curious as to why you would like it, because:

 MyClass
(returns the MyClass class representation)
 MyClass()
(returns a instance of the MyClass class)

So, are you just looking for a method that does exactly the above?

Best regards,

Ching-Yun Xavier Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com
Website: http://xavierho.com/


On Wed, Jun 17, 2009 at 8:39 PM, Bruno Desthuilliers
bruno.42.desthuilli...@websiteburo.invalid wrote:

 Paul Johnston a écrit :

 Hi,

 I would like to have a method that is both a classmethod and an
 instancemethod. So:

 class MyClass(object):
  @class_or_instance
  def myfunc(cls_or_self):
pass

 The semantics I'd like are:
 When you call MyClass.myfunc, it gets passed a class
 When you call MyClass().myfunc, it gets passed an instance

 I'm sure I've seen some code to do this somewhere, but I can't find it
 now. Any help appreciated.


 IIRC, there's something quite similar in formencode.


 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list