Neil Zanella wrote:
Hello,
In Python, classes are objects. But there is no way to custom print a
class object. This would require some syntax such as the one commented out
below: With the current "foo = classmethod(foo)" mechanism custom printing
for class objects is not possible.
#!/usr/bin/python
class Foo:
def __str__(self):
return "foo"
#def classmethod __str__(cls):
# return "pythons bite"
foo = Foo()
s = "hello %s!" % foo # custom text here
print s
print Foo # no custom text here possible it seems, unless we call
# a staticmethod such as Foo.printMe()
Regards,
Neil
Classes are objects. You have to define the __str__() method in the object's
class - for a class that would be the metaclass. Now here:
... def __str__(self):
... return "custom text for class %s" % self.__name__
...
... __metaclass__ = FooType
... def __str__(self):
... return "custom text for %s instance" %
self.__class__.__name__
...
custom text for Foo instance
custom text for class Foo
>>>
Peter
From http Fri Aug 20 20:44:00 2004
From: http (Paul Rubin)
Date: 20 Aug 2004 11:44:00 -0700
Subject: decorator J4 - any objections?
References: <
[email protected]>
Message-ID: <
[email protected]>
JimJJewett at yahoo.com (Jim Jewett) writes:
<URL:
http://www.python.org/moin/PythonDecorators > (section 5.21 J4)
looks very good to me -- and it is the only alternative without negatives.
def func(arg1, arg2)
@version("Added in 2.4")
@returns(None)
as:
"""Docstring could be here, or in decorator part above"""
# body goes here
What was wrong with using . or - instead of @ ? Given that this is
new syntax, just about any character could work. Or what about no
special punctuation at all? Using @ makes me cringe somewhat.
Also, why the need for the "as" keyword? What happens if it's just
eliminated? I.e.:
def func(arg1, arg2)
@version("Added in 2.4")
@returns(None):
"""Docstring could be here, or in decorator part above"""
# body goes here