FAQ
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

Search Discussions

  • Reinhold Birkenfeld at Aug 20, 2004 at 6:30 pm

    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()
    You need Metaclasses for that. Consider:
    class PrintTest(object):
    ... class __metaclass__(type):
    ... def __str__(self):
    ... return "I'm a PrintTest"
    ...
    print PrintTest
    I'm a PrintTest
    >>>

    Reinhold

    --
    Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
    mitbr?chte, w?re das bedauerlich. Was bei Windows der Umfang eines
    "kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
    -- David Kastrup in de.comp.os.unix.linux.misc
  • Mike C. Fletcher at Aug 20, 2004 at 6:36 pm
    From my metaclasses presentation:

    """Simple example of changing class repr"""
    class Meta( type ):
    def __repr__( cls ):
    return '<OhLookAMetaClass>'
    class X:
    __metaclass__ = Meta

    # this uses the meta-property for lookup
    assert repr(X) == '<OhLookAMetaClass>'

    Code and presentation available at:
    http://www.vrplumber.com/programming/

    HTH,
    Mike

    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.
    ...

    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://www.vrplumber.com
    http://blog.vrplumber.com
  • Peter Otten at Aug 20, 2004 at 6:40 pm

    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:
    class FooType(type):
    ... def __str__(self):
    ... return "custom text for class %s" % self.__name__
    ...
    class Foo:
    ... __metaclass__ = FooType
    ... def __str__(self):
    ... return "custom text for %s instance" %
    self.__class__.__name__
    ...
    print Foo()
    custom text for Foo instance
    print Foo
    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: <cab22418.0408201006.16f163b2@posting.google.com>
    Message-ID: <7xbrh5hcpb.fsf@ruckus.brouhaha.com>

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 20, '04 at 6:11p
activeAug 20, '04 at 6:40p
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase