0

for example,

extending the string class so that printing it will prepend it with a tab or some white space. Then being able to call it with,

print 'hello world'

Which when seen on the stdout would be prepended with tabs(white space).

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
scruffyDog
  • 721
  • 3
  • 7
  • 17

2 Answers2

3

Yes: Edit the source code of Python.

Seriously: Like any other programming model (structured, OO, modeling), Python imposes some limitations on what you can do. While some of those might feel arbitrary or "stupid", there is a good reason for each of them (usually, the drawbacks of all the other solutions or a mistake in the past which can't be fixed anymore without breaking all the existing code).

When you feel that you need to break the rules, take a step back and consider this: Maybe your "solution" is wrong? Maybe there is a better way to achieve what you want?

In this case, use formatting:

f = '\t%s'
print f % 'hello world'

Unlike your solution, this explicitly communicates what you try to achieve.

That said, you can use a technique used "monkey patching." Note that this technique is dangerous. And in most cases, it means that the person using the technique didn't know what they were doing.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Is it actually possible to monkey patch built-ins? When I try assigning `str.__str__` to something else, I get `TypeError: can't set attributes of built-in/extension type 'str'`. Not that this is a good idea -- I agree with your warning -- I just tried, and ask, out of curiosity about how Python works. – Ghopper21 Aug 15 '12 at 10:25
  • 1
    I'd recommend going with the new [`str.format`](http://docs.python.org/library/string.html#format-examples) formatting if you're not already hooked on the old `%` formatting. It's an improved replacement, after all. Like this: `"\t{0}".format("hello world")`. – Lauritz V. Thaulow Aug 15 '12 at 10:25
  • Thanks, monkey patching sounds interesting. The above was just an example, I didn't want to go into too much detail about what I was doing. I just wanted to know what is possible with this language – scruffyDog Aug 15 '12 at 10:31
  • 1
    @Ghopper21: This is another problem with monkey patching: It doesn't work for all types. – Aaron Digulla Aug 15 '12 at 10:35
  • Monkeypatching does not work for built-ins. It does so, and is abused for this, in Ruby, but not in Python. – jsbueno Aug 15 '12 at 11:24
3

Do you want to change the string class, or is it just the behaviour of print that you want to change? If you:

from __future__ import print_function

(or use Python 3) you can provide your own print function, for example:

def print(*line, **kwargs):

    args = dict(sep=' ', end='\n', file=None)
    args.update(kwargs) 

    sep  = args['sep']
    end  = args['end']
    file = args['file']

    newlist = [];

    for userln in line:
        newlist.append("\t"+userln)  # prefix the line with a tab

    if file == None:
        file = sys.stdout

    __builtin__.print(sep.join(newlist),end=end,file=file)
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • I think that was just an example, he's asking a more general question about the different possibilities of extending Python classes. – CODe Aug 15 '12 at 12:37