0

I have a class which outputs a lot of textchunks, depending on an initial parameter I want it to justify the text right/left/center. I was wondering if it is possible to use a method object (http://docs.python.org/py3k/tutorial/classes.html#method-objects) instead of a function like

class textOutput(object):
    def __init__(self):
        self.justification = "left"

    def justify(self,text):
        if self.justification == "right":
            return text.rjust(self._width)
        elif self.justification == "center":
            return text.center(self._width)
        else:
            return text.ljust(self._width)

    def output(self):
        for i in range(1 to 1000000):
            print self.justify(i)

i would like to use a function object like this (replaceing the justify method above)

class textOutput(object):
    def __init__(self):
        self.justify = string.ljust

    def output(self):
        for i in range(1 to 1000000):
            print self.justify(i,self._width)

    def setJustification(self, justification):
        if justification == "right":
            self.justify = string.rjust
        elif justification == "center":
            self.justify = string.center
        else:
            self.justify = string.ljust

however I do not know i get functionobjects for the string-datatype members.

How can I achieve the later? (The former has so many unneccessary checks going against performance)

p.s.: I have to use jython which is basically python 2.5

p.p.s.: The justification switching would then be done witht he following class method:

5
  • That's not how classes and methods are used in Python. And you cannot define a module global __init__ method. Commented May 7, 2012 at 15:56
  • these are excerpts. yes the class name(baseobject): part is missing, but I think that is not needed here. Commented May 7, 2012 at 16:00
  • 1
    So you should add it, so that we can understand what are you trying to accomplish. Commented May 7, 2012 at 16:03
  • What is the problem that you are having? This looks to me like it would work. Have you tried it and are getting an exception, or are getting something different than you want? Commented May 7, 2012 at 16:21
  • @DavidRobinson: The problem is i need a handle to the general String object, not a string i have defined, if I try it on the shell (x=string.ljust) it tells me that string / String is not defined. do I have to import a module? Or what is the name for the object? Commented May 7, 2012 at 16:31

2 Answers 2

1

You can either do

import string

at the beginning of the file, or use

str.ljust
str.rjust
str.center

instead of the corresponding usages of string. Also, you don't call setJustification in the current code.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer, I will try it tomorrow, and accept it if it works. P.S.: I used setJustification in the initialization now, and it is intended as a setter used by the user of the class, thanks for the additional input
ok it works, i fixed one additional bug with my code above, thanks a bunch
0

I hope you looked at getattr method to grab the function object from string. The following line in your code should get ur stuff working

{ self.justify = getattr(' ', 'ljust', None)}

1 Comment

i think this will just allow me to justify an empty string, DavidRobinsons solution is what i was looking for. thank you for your effort though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.