Python-repr () and Val () functions

Source: Internet
Author: User
Tags gety integer numbers string format

1. The repr () function transforms the object into a form for the interpreter to read. Grammar

The following is the syntax for the Repr () method:

Repr(object)  
Parameters
    • Object--Objects.
return value

Returns the string format of an object.

Both STR and repr are used to convert numbers, lists, and other types into strings, but the difference is that STR is much more like the output of printf in C, and the content of the REPR output directly displays the type of the variable, as you can see, For variables with obvious type flags, the conversion of STR and repr are significantly different, such as long numbers and string ' symbols, and there is not much difference between record data, such as integer numbers, that do not have very large differences.

2. The eval (str) function is powerful, officially interpreted as: evaluates the string str as a valid expression and returns the result of the calculation. So it's good to combine math as a calculator.

The most common functions of the eval () function are:
1. Evaluates a valid expression in a string and returns the result

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

2. Convert the string to the corresponding object (such as the conversion between list, tuple, dict, and string)

"[[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, the string that will be converted with an anti-quote is then reversed back to the object

>>> list1 = [1,2 ,3,4,5]>>>  List1 '  [1, 2, 3, 4, 5] ' >>> type ( ' List1 ') <type  ' str ' >>>> type (eval ( ' List1 ')) <type Span class= "hljs-string" > ' list ' >>>> a = eval ( ' List1 ') >>> A[1, 2, 3, 4, 5] 


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

    1. Add an __eq__ method which returns True if coordinates refer to same point in the plane (i.e., with the same x and Y Coordin ATE).

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

< Span class= "Hljs-number" > &NBSP;         
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)
 

Python-repr () and Val () functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.