English 中文(简体)
阶级继承和印刷/印刷方法
原标题:Class inheritance and the print/_str_() methods

我再次对提出另一个非常次要的问题表示歉意。 出于某种原因,我似乎无法从贾瓦向沙勒过渡。 我认识到,在帮助使用者做事的方法中,沙捞越了这些东西。 但是,我很想知道,以下两部法典是否相同,或者说“更正式”的意思是,在 Java使用比在 other使用更为常见。

def print()
    print "My name is___"

以及

def _str_()
   print "My name is___"

Once again, either one of my methods might be completely wrong (my guess would be the "str" methods. In the interest of time, I have not written out the class name 以及other methods/attributes. Thanks ahead of time.

最佳回答

页: 1

<代码>__str__功能在班级使用。 如果某一物体被转换成扼杀物,则该功能被称作。 诸如<编码>印有/编码”等功能在内部进行。 也可打电话到<代码>str(my_object_instance)。

<>注> 如同在被占领土上一样,只有一点是特别的。


实例(Python互动):

>>> class A:                                                                                        
...   def __str__(self):                                                                            
...     return "I am an instance of A"                                                              
...                                                                                                 
>>> a_inst = A()                                                                                    
>>> print a_inst                                                                                    
I am an instance of A                                                                               
>>>  

如果有任何问题,请作评论。

问题回答

Any __xxx___ has a special meaning, one seasoned pythonneer expect xxx(obj1) to calls obj1.__xxx__().

但有时议定书使用的关键词是:

即便它有特殊(反常)的含义,执行也像任何其他方法一样,因此,它可能受到后代的统治。

The goals behind that methods are both legit (the sintax not too much), but which one to choose depends on what you re trying to do.

也许简单的例子将有助于澄清一些事情。

请界定<代码>。 用户类别:

class User:

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return "My name is %s" % self.name

    def show(self):
        print "My name is %s" % self.name

<>说明: 我将方法名称从<条码>改为<条码>。 因为在python 2.x,是关键词,因此你可以使用。

Now let s try the User class to see the difference between __str__ and show?:

>>> rik = User( Ricky )
>>> rik.show()
My name is Ricky
>>> rik.__str__()
 My name is Ricky 
>>> print rik
My name is Ricky

守则应自行解释。

__str__ (here the documentation)

页: 1 如果对这种方法加以界定,则其名称为.__str__(),并载明将印制收益。

Main point __str__:

  • should not call print
  • and must return a string.

show

另一方面,show(>)直接印刷插图(和<>None)。 这就是说。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签