资源描述:
《Python for Ruby Programmers.pdf》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、PythonforRubyProgrammersMIKELEONELARUBYCONF2013FEB232013Thispresentation:1.Fast-paced2.Pythonin30mins3.It'seasy!AboutMeWebMobileEmbeddedThat'sit.Ruby:2006Python:2010NewappreciationBetterRubyistWhatwe'llcover1.Exampleprogram2.13Designsimilarities3.13Differences4.5Problemdomains5.“Feelings”WhatweWON'
2、Tcover1.Implementations2.Performance(Goodenough)3.Parallelism(GIL)4.Minorsyntax(You'llgetit)WhatI'lltrytoconvinceyou:1.VERYsimilar!2.You'realreadyready3.Python=BetterRubyist4.HireaPythonistaZenofPython(abridged)1.Explicitisbetterthanimplicit.2.Flatisbetterthannested.3.Readabilitycounts.4.Thereshoul
3、dbe[only]oneobviouswaytodoit.5.Namespacesareagreatidea--let'sdomoreofthose!Example:CreateaPersonclass1.Nameandage2.Cangreetyou3.Knowsifit'sBeiber4.GreetsGoogledirectors13Similarities1.DynamicallyTypeddefprint_object(some_sorta_object):printsome_sorta_object.read()print_object(my_file)>>>“Thisisthef
4、ilecontents”2.EverythingisanObject(evenfunctions!)Pythonexample:5+3>>>8(5).__add__(3)>>>8(5).__str__()>>>“5”3.ArraysRuby(array)things=[1,"hello",2.5,43]things[1]>>'hello'Python(list)things=[1,"hello",2.5,43]things[1]>>'hello'4.HashTablesRuby(hash)color_map={red:"#FF0000",purple:"#800080"}color_map[
5、:red]>>"#FF0000"Python(list)color_map={"red":"#FF0000","purple":"#800080"}color_map["red"]>>"#FF0000"5.Nospeciallineterminators(nosemicolons!)6.StrongfunctionalprogrammingparadigmsMapdeftriple(number):returnnumber*3list=[1,2,3,4]map(triple,list)>>>[3,6,9,12]Filterdefis_even(number):returnnumber%2==
6、0list=[1,2,3,4]filter(is_even,list)>>>[2,4]AnonymousFunctions(lambda)list=[1,2,3,4]map(lambdax:x*3,list)>>>[3,6,9,12]7.AwesomefunctionparametersOptional/namedargumentsdefgreet(name,greeting="Hello"):printgreeting+","+namegreet("Bob")>>>Hello,Bobgreet("Bob",greeting="Bonjour")>>>Bonjour,Bobgreet("Bo
7、b","Bonjour")>>>Bonjour,BobArgumentUnpackingdefgreet_everyone(*names):fornameinnames:print"Hello,"+namenames=["Bob","Steve","Jim"]greet_everyone(*names)Hello,BobHello,SteveHello,Jim8.RaisingExceptionsRubyin