7

My understanding of the print() in both Python and Ruby (and other languages) is that it is a method on a string (or other types). Because it is so commonly used the syntax:

print "hi"

works.

So why doesn't "hi".print() in Python or "hi".print in Ruby work?

DorkRawk
  • 732
  • 2
  • 6
  • 21
  • if you gave a string object a print method, why would you stop there, every object in the system should then a have a print method. Printing does strike me as not a particularly string like operation, compared with slicing, searching etc... Print is just a particular form of I/O, and you don't see objects like integers and strings with a write method that takes a file handle as an argument. – Tim Hoffman Sep 05 '12 at 01:04
  • 2
    Why has this question gotten so many upvotes when it's effectively "why can't I type random crap and have it run"? When did this become about approaching a language from _complete ignorance_? – Matthew Trevor Sep 05 '12 at 01:11
  • @MatthewTrevor: take it easy. The question is about the logic behind what becomes a method and what becomes a global function, etc. – Ned Batchelder Sep 05 '12 at 01:27
  • 1
    That's a far more generous interpretation of this question than I can manage. – Matthew Trevor Sep 05 '12 at 01:31
  • Hey guys, I misunderstood how something worked. Some nice people corrected me and explained my mistake. Now I know. That seems like a good thing. – DorkRawk Sep 05 '12 at 02:41
  • 1
    @MatthewTrevor considering echristopherson's answer, I don't think this is a stupid question. – Andrew Grimm Sep 05 '12 at 23:07

7 Answers7

9

When you do something like "hi".print(), you are implying that the string object "hi" has a method print. This is not the case. Instead, print is a function that takes a string (or other types) as input.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
guyrt
  • 927
  • 7
  • 12
  • 2
    This is only true for Python. Ruby doesn't have functions, only methods. In Ruby, `print` is a method of the `IO` protocol, implemented by, for example, `IO#print` and `StringIO#print`. Also, there is a convenience method `Kernel#print`, which basically calls `$>.print`, where `$>` is a global variable denoting the default output IO stream (usually, `$>` points to `STDOUT`, but it doesn't have to). It is this convenience method that allows you to say just `print 'Hi'` instead of `$>.print 'Hi'` or `STDOUT.print 'Hi'`. – Jörg W Mittag Sep 05 '12 at 01:08
4

Ruby does have a method Object#display (doc here), which sends a representation of the object to the current output stream, or one specified as an argument.

(I find that it's hard to work with in irb if I use ; at the end of a line to suppress the printing of the return value; if I do that, display's output isn't shown, even if I flush the stream.)

echristopherson
  • 6,974
  • 2
  • 21
  • 31
  • Thumbs upping this since I didn't know about `Object#display`, even though I think I dislike its existence since it makes no sense that any given object magically knows how to print itself (because a: how should it look? like self.to_s or self.inspect, etc, and b: how does it know where to print itself to? $stdout or $stderr or some random file or socket or what? What a terrible dependency for every single object to need to know about streams) – Joshua Cheek Sep 06 '12 at 05:19
  • @Joshua Cheek: if you don't specify a stream to output to, it uses the value of the pseudoglobal `$>`, which I didn't even know existed until I looked it up. – echristopherson Sep 06 '12 at 18:43
2

Why should it work? String classes rarely have void print methods - and you would never need them, because the standard static print function can print those strings anyway. It is important to note: method(someObject) is not necessarily the same as someObject.method().

arshajii
  • 127,459
  • 24
  • 238
  • 287
2

It's not a method on a string. Prior to Python 3, it was a statement (just like break or import), and you could use both print "hi" and print("hi"). From Python 3, it was replaced by a function, thus you can no longer use print "hi":

Print Is A Function

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105).

João Silva
  • 89,303
  • 29
  • 152
  • 158
2

What do you propose str.print should do?

print to stdout? how about stderr? or a file? or a serial port?

Printing to stdout is really a special case but it's so ubiquitous that sometimes it can be overlooked.

Then we'd have to specify where str should print to every time we create a string?

At the very least we'd have to say

"foo".print(sys.stdout)

Hopefully that looks awful to you too. It's a confusion of responsibilities

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

print isn't a method on a string in Python (or in Ruby, I believe). It's a statement (in Python 3 it's a global function). Why? For one, not everything you can print is a string. How about print 2?

li.davidm
  • 11,736
  • 4
  • 29
  • 31
-1

In case you are more happy to use a method rather than a statement in Ruby you can use the method display ("test".display) to achieve this or define a new method easily like

class String
  def print
    puts self
  end
end

and use it like this

"test".print
peter
  • 41,770
  • 5
  • 64
  • 108
  • Monkey patching objects like this is a generally bad habit to be in, and especially in cases like this where you're creating huge dependencies. I know it won't bite you in this case since print is in Kernel, but that's just an exception (that an entire blog post could be written about). – Joshua Cheek Sep 06 '12 at 05:27
  • everybody has the right to his opinion and i see no dependensies here other then when you use a method it has to be defined somewhere in your code – peter Sep 06 '12 at 09:13
  • It depends on an IO stream and the global variable `$stdout`. – Joshua Cheek Sep 06 '12 at 12:24