python-repr()和val()函數


1. repr() 函數將對象轉化為供解釋器讀取的形式。

語法

以下是 repr() 方法的語法:

repr(object)

參數

  • object -- 對象。

返回值

返回一個對象的 string 格式。

str和repr都是用來將數字,列表等類型轉化為字符串的形式,但不同之處在於str更加類似於C語言中使用printf輸出的內容,而repr輸出的內容會直接將變量的類型連帶着表現出來,從下圖可以看出,對明顯帶有類型標志的變量而言,str和repr的轉換具有明顯的差別,如long型數字和字符串的‘’符號,而對於並沒有非常大區別的記錄數據如整型數字,二者並沒有太大的差別。

 

2. eval(str)函數很強大,官方解釋為:將字符串str當成有效的表達式來求值並返回計算結果。所以,結合math當成一個計算器很好用。

eval()函數常見作用有: 
1、計算字符串中有效的表達式,並返回結果

>>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> eval("n + 4") 85 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、將字符串轉成相應的對象(如list、tuple、dict和string之間的轉換)

>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" >>> b = eval(a) >>> b [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> a = "{1:'xx',2:'yy'}" >>> c = eval(a) >>> c {1: 'xx', 2: 'yy'} >>> a = "(1,2,3,4)" >>> d = eval(a) >>> d (1, 2, 3, 4) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3、將利用反引號轉換的字符串再反轉回對象

>>> list1 = [1,2,3,4,5] >>> `list1` '[1, 2, 3, 4, 5]' >>> type(`list1`) <type 'str'> >>> type(eval(`list1`)) <type 'list'> >>> a = eval(`list1`) >>> a [1, 2, 3, 4, 5]


e.g. Your task is to define the following two methods for the Coordinate class:

  1. Add an __eq__ method that returns True if coordinates refer to same point in the plane (i.e., have the same x and y coordinate).

  2. Define __repr__, a special method that returns a string that looks like a valid Python expression that could be used to recreate an object with the same value. In other words, eval(repr(c)) == c given the definition of __eq__ from part 1.

 
class Coordinate(object):
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def getX(self):
        # Getter method for a Coordinate object's x coordinate.
        # Getter methods are better practice than just accessing an attribute directly
        return self.x

    def getY(self):
        # Getter method for a Coordinate object's y coordinate
        return self.y

    def __str__(self):
        return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'

    def __eq__(self, other):
        # First make sure `other` is of the same type 
        assert type(other) == type(self)
        # Since `other` is the same type, test if coordinates are equal
        return self.getX() == other.getX() and self.getY() == other.getY()

    def __repr__(self):
        return 'Coordinate(' + str(self.getX()) + ',' + str(self.getY()) + ')'



Test: equal 1

 
         
Output:
> print(c1)
<1,-8>
> print(c2)
<1,-8>
> print(c1 == c2)
True

Test: equal 2

 
         
Output:
> print(c1)
<20,20>
> print(c2)
<20,20>
> print(c1 == c2)
True

Test: not equal 1

 
         
Output:
> print(c1)
<-16,-4>
> print(c2)
<14,20>
> print(c1 == c2)
False

Test: not equal 2

 
         
Output:
> print(c1)
<7,13>
> print(c2)
<-2,-1>
> print(c1 == c2)
False

Test: repr

 
         
Output:
> print(c1)
<17,38>
> print(repr(c1))
Coordinate(17,38)

Test: repr randomized

 
         
Output:
> print(c1)
<-12,-20>
> print(repr(c1))
Coordinate(-12,-20)
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM