为什么我不能“字符串”.print()?

时间:2022-09-19 06:51:48

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:

我对Python和Ruby(以及其他语言)中print()的理解是它是一个字符串(或其他类型)的方法。因为它是如此常用的语法:

print "hi"

works.

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

那么为什么不用Python中的“hi”.print()或Ruby中的“hi”.print工作呢?

7 个解决方案

#1


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.

当您执行类似“hi”.print()的操作时,您暗示字符串对象“hi”具有方法print。不是这种情况。相反,print是一个以字符串(或其他类型)作为输入的函数。

#2


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.

Ruby确实有一个方法Object#display(doc here),它将对象的表示发送到当前输出流,或者指定为参数。

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

(我发现如果我使用irb很难处理;在一行的末尾抑制返回值的打印;如果我这样做,即使我刷新流,也不会显示显示的输出。)

#3


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":

它不是字符串上的方法。在Python 3之前,它是一个声明(就像中断或导入一样),你可以使用print“hi”和print(“hi”)。从Python 3开始,它被一个函数替换,因此您不能再使用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).

print语句已替换为print()函数,其中包含用于替换旧打印语句(PEP 3105)的大多数特殊语法的关键字参数。

#4


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

它为什么要起作用?字符串类很少有void print方法 - 你永远不需要它们,因为标准的静态打印函数无论如何都可以打印这些字符串。需要注意的是:method(someObject)不一定与someObject.method()相同。

#5


2  

What do you propose str.print should do?

你建议什么str.print应该做什么?

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

打印到标准输出? stderr怎么样?还是档案?还是一个串口?

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?

那么我们每次创建字符串时都必须指定str应该打印的位置?

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

希望你看起来也很糟糕。这是责任的混乱

#6


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?

print不是Python中字符串的方法(或者我相信在Ruby中)。这是一个声明(在Python 3中它是一个全局函数)。为什么?首先,并非您可以打印的所有内容都是字符串。打印2怎么样?

#7


-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

如果你更乐意在Ruby中使用方法而不是语句,你可以使用方法display(“test”.display)来实现这一点,或者很容易定义一个新方法

class String
  def print
    puts self
  end
end

and use it like this

并像这样使用它

"test".print

#1


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.

当您执行类似“hi”.print()的操作时,您暗示字符串对象“hi”具有方法print。不是这种情况。相反,print是一个以字符串(或其他类型)作为输入的函数。

#2


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.

Ruby确实有一个方法Object#display(doc here),它将对象的表示发送到当前输出流,或者指定为参数。

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

(我发现如果我使用irb很难处理;在一行的末尾抑制返回值的打印;如果我这样做,即使我刷新流,也不会显示显示的输出。)

#3


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":

它不是字符串上的方法。在Python 3之前,它是一个声明(就像中断或导入一样),你可以使用print“hi”和print(“hi”)。从Python 3开始,它被一个函数替换,因此您不能再使用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).

print语句已替换为print()函数,其中包含用于替换旧打印语句(PEP 3105)的大多数特殊语法的关键字参数。

#4


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

它为什么要起作用?字符串类很少有void print方法 - 你永远不需要它们,因为标准的静态打印函数无论如何都可以打印这些字符串。需要注意的是:method(someObject)不一定与someObject.method()相同。

#5


2  

What do you propose str.print should do?

你建议什么str.print应该做什么?

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

打印到标准输出? stderr怎么样?还是档案?还是一个串口?

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?

那么我们每次创建字符串时都必须指定str应该打印的位置?

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

希望你看起来也很糟糕。这是责任的混乱

#6


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?

print不是Python中字符串的方法(或者我相信在Ruby中)。这是一个声明(在Python 3中它是一个全局函数)。为什么?首先,并非您可以打印的所有内容都是字符串。打印2怎么样?

#7


-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

如果你更乐意在Ruby中使用方法而不是语句,你可以使用方法display(“test”.display)来实现这一点,或者很容易定义一个新方法

class String
  def print
    puts self
  end
end

and use it like this

并像这样使用它

"test".print