By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,565 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

Print String

P: n/a
Hello Eveybody,

I have written a method which prints the prefix notation of any expression.

here is the method...

def PrintPrefix(expr):
if expr.__class__==E:
print expr.operator,
PrintPrefix(expr.left),
PrintPrefix(expr.right),
else:
print expr,

Now if i pass an expression to it like e1=x+y+z:

I call it by passing PrintPrefix(e1):
The output is ++xyz

now i want to store this in a string...

Can anyone help....

Balaji
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I'd try something along these lines:
def GeneratePrefix(expr):
if expr.__class__ == E:
yield str(expr.operator)
for i in GeneratePrefix(expr.left): yield i
for i in GeneratePrefix(expr.right): yield i
else:
yield str(expr)
The successive elements produced by this (untested) code should be the
same as the elements printed by your code.

To join these into a string without any whitespace, you could write
def StringPrefix(expr):
return "".join(GeneratePrefix(expr))
and you could re-write the original PrintPrefix as
def PrintPrefix(expr):
for item in expr:
print item,

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFAvMn/Jd01MZaTXX0RAgZnAKCUjKB5SigE/kXtjCVs4jNfRlsx2wCfcJkS
3LeJ0Tbl0hYuAa5aMUiRIa0=
=KSG8
-----END PGP SIGNATURE-----

Jul 18 '05 #2

P: n/a
Balaji wrote:
I have written a method which prints the prefix notation of any
expression.

here is the method...
The convention is to call it "method" only if it is part of a class, so what
you show is a "function".
def PrintPrefix(expr):
if expr.__class__==E:
Explicit class checks are a bad idea. I'd rather let the class decide how it
wants to print or convert an instance to string - i. e. I would prefer
methods over a function to implement this feature.
print expr.operator,
PrintPrefix(expr.left),
PrintPrefix(expr.right),
else:
print expr,

Now if i pass an expression to it like e1=x+y+z:

I call it by passing PrintPrefix(e1):
The output is ++xyz

now i want to store this in a string...


Below is a minimal example that uses __str__() (the method that is
implicitely called by the str() function) to produce infix notation and
__repr__() (corresponding to repr()) for prefix notation. Just say

print repr(e)

to print in prefix notation and

s = repr(e)

to store it as a string.

class Base(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
__repr__ = __str__
def __add__(self, other):
return Expression(Operator("+"), self, other)

class Operator(Base):
pass

class Variable(Base):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name

class Expression(Base):
def __init__(self, operator, left, right):
self.operator = operator
self.left = left
self.right = right
def __str__(self):
""" infix """
return "%s%s%s" % (self.left, self.operator, self.right)
def __repr__(self):
""" prefix """
return "%r%r%r" % (self.operator, self.left, self.right)

x = Variable("x")
y = Variable("y")
z = Variable("z")
e = x + y + z
print repr(e)
print str(e)
Peter

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.