python--Script (Calculator)

Source: Internet
Author: User

Basic Python Tutorial (second edition) P123

The code of the book is as follows:

1 classCalculator:2     defCalculator (self,expression):3Self.value = eval (expression)#eval () is a function4 5 classTalker:6     defTalk (self):7         Print "hi,my Value is", Self.value8 9 classTalkingcalculator (calculator,talker):Ten     Pass

Then enter in the code line:

>>TC = Talkingcalculator ()

>>tc.calculator (' 1+2*3 ')

>>tc.talk ()

Output: Hi,my value is 7

The following code changes to the script:

1 #!/usr/bin/env python2 #Coding=tuf83 4 classCalculator:5     defCalculator (self,expression): #函数定义 () are all commas ', ' 6Self.value = eval (expression)#eval () is a function7 8 classTalker:9     defTalk (self):Ten         Print "hi,my Value is", Self.value One  A classTalkingcalculator (calculator,talker): -     Pass -  theTC =Talkingcalculator () -Tc.calculator ('1+2*3')#the input is a string -Tc.talk ()

In this example:

1. Note that the string entered in Tc.calculator ('1+2*3') instead of a number, returns the result as a number.

The reason is that eval () is a function--eval parameter is a string that can be evaluated as an expression. The expression must be a string that is quoted ('), regardless of the form of the input

The eval () function can manipulate numbers, letters, strings, lists, tuples, and limit the operation of the dictionary.

For example:

>>> A = {'a':'am'B:'Baby'}#string to be quoted (')Traceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'b'  is  notdefined>>> A = {'a':'am','b':'Baby'}>>> Eval (a)#eval () the expression in parentheses must be a string (quoted ")Traceback (most recent): File"<stdin>", Line 1,inch<module>typeerror:eval () arg1 must be a stringorCode Object>>> Eval ('a'){'a':'am','b':'Baby'}>>> x = {'a':'am','b':'Baby'}>>>eval (x) Traceback (most recent call last): File"<stdin>", Line 1,inch<module>typeerror:eval () arg1 must be a stringorCode Object>>> Eval ('x'){'a':'am','b':'Baby'}>>> y = eval ('x')>>>y{'a':'am','b':'Baby'}>>> m = {'C':'Crumb'}>>> Eval ('x+m')#dictionaries cannot be addedTraceback (most recent): File"<stdin>", Line 1,inch<module>File"<string>", Line 1,inch<module>typeerror:unsupported operand type (s) for+:'Dict'  and 'Dict'>>> x = 1>>> y = 2>>> eval ('X+y')#numbers can3>>> x ='a'>>> y ='b'>>> Eval ('X+y')#string can (letter can)'AB'>>> x = ['a','b']        >>> Eval ('x')['a','b']>>> y = ['C','D']>>> Eval ('X+y')#list [] to['a','b','C','D']>>> x = ('a','b')>>> Eval ('x')('a','b')>>> y = ('C','D')>>> Eval ('X+y')#tuples () can('a','b','C','D')>>> x ='Hello'>>> y =' World'>>> Eval ('X+y')#string can be'HelloWorld‘

>>> A = 1
>>> eval (' a+1 ')
2
>>> eval (' A = = 1 ') #可以进行判断
True
>>> eval (' A = = 0 ')
False

Other references to the Eval function: WWW.TUICOOL.COM/ARTICLES/BBVNQBQ

Parameter self: only available in class, individual Def script does not have self

      Classes (class) are initialized to get an instance (instance). Self is the instance that is used to represent the initialization.
Explicitly write a self parameter so that the method of the class and the normal function (functions) do not differ in nature, and all input parameters are shown to be passed to the method/function.
Of course, as a method of the class, the object must be an instance, so at the beginning of the design of Python can be designed to be self not as a parameter, but then need a keyword to represent the instance, such as in JavaScript is this.
However, the philosophy of Python is that "Explicit is better than implicit.", the display is better than implicit, so the Python class method requires a self parameter to represent the instance as logical.

The following script: There is no self in the following code

1 #!/usr/bin/env python2 #!coding=utf83 """4 fibs = [0,1]5 For I in range (8):6 fibs.append (fibs[-2]+fibs[-1])7 Print fibs8 """9 Tenfibs = [0,1] Onenum = input ('What are you num:') A  forIinchRange (num-2): - #fibs = [0,1] -Fibs.append (fibs[-2]+fibs[-1]) the PrintFib

python--Script (Calculator)

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.