从c++转到Python需要注意的地方

下面的资料,会随着学习的不断深入,持续的更新。

内容没有能有效的组织,因为没一点是本人再学习的过程中,慢慢的总结出来的。等本篇文字足够长的时候,可能会考虑组织文件可读性更强的文字。

 

1. c/c++里面,注释代码是用 // ,而python是用 #

 

 2. c/c++里面,定义类对象

 

[html] view plain   copy   print   ?  
  1. // define class  
  2. class ClassA  
  3. {  
  4. ...  
  5. };  
  6. //  
  7. ClassA OA; // 对象  
  8. ClassA *pA = new ClassA; // 指针  
// define class
class ClassA
{
...
};
//
ClassA OA; // 对象
ClassA *pA = new ClassA; // 指针



而python里面

[html] view plain   copy   print   ?  
  1. # define class  
  2. class ClassA:  
  3.     def func( self, name ):  
  4.         #do something  
  5.   
  6.    
  7. # declare a object of the class  
  8. ca = ClassA()  
  9.   
  10. ca.func( 'myname' )  
# define class
class ClassA:
    def func( self, name ):
        #do something

 
# declare a object of the class
ca = ClassA()

ca.func( 'myname' )



 

3.  c/c++主要依靠语句开始结束来判定语句块,如 “{ } , ;”这些。 而python靠的是缩进,缩进四个空格,不是一个tab,因为tab根据所使用的文本编辑不一样,所代表的空格数量不一样,有些是4,有些是8.

 

4. python类的成员函数都必须有一个self的参数传进去,self相当于c/c++的this。也就意味着成员函数至少有一个参数,不想c/c++可以没有参数。

 

5. 继承语法不一样,下面看下的python的类继承。

[python] view plain   copy   print   ?  
  1. class ClassA:  
  2.     def funca( self, name ):  
  3.         #do something   
  4.   
  5. class ClassB(ClassA):  
  6.     def funcb( self, name ):  
  7.         #do something  
class ClassA:
    def funca( self, name ):
        #do something

class ClassB(ClassA):
    def funcb( self, name ):
        #do something


 6. python没有main函数,我们的c/c++都是以main为入点函数。但是python没有入点函数,而是从py文件的第一行就开始执行

 

7. c/c++里面字符串是用双引号""来修饰,而python则是用单引号 '', 或者是双引号""

[python] view plain   copy   print   ?  
  1. char *p = "hello";  
  2.   
  3. p='hello'  
char *p = "hello";

p='hello'


 

8.  打印语句,print在2.7以前的版本print可以写成:

print 'hello world'

或者

print ( 'hello world' )

但是2.7以后第一种格式不被支持。

 

///

下面给些样例代码,windows下面这些代码你可以直接ctrl+c, ctrl+v到你的记事本,然后保存成以py为后缀的文件你就可以直接执行;linux下面将下面的这些代码拷贝到一个新建的以py为后缀的文件,在文件的最开始加上#!/usr/bin/python,这行代码其实就是指定要用那个python解释器去执行该脚本。

 

[python] view plain   copy   print   ?  
  1. <PRE class=python name="code"># test simple print   
  2. print 'hello world'  
  3. name = 'myname'  
  4. print 'hello %s' %name  
  5.   
  6. # define variables   
  7. max=10000  
  8. min=1  
  9. useridbase='2012FFD_'  
  10.   
  11. # define a function to print string  
  12. def MyPrint( str ):  
  13.  print str  
  14.    
  15. # define a function to do somthing  
  16. def UpdateUserID( id ):  
  17.  idd = id + 1  
  18.  idCur = '%s' %idd  
  19.  useridCurrent = useridbase+idCur  
  20.  print 'user name: %s' %useridCurrent  
  21.  return id+1  
  22.   
  23. # define class   
  24. class cTest1:  
  25.  def func1( self, name ):  
  26.   print 'hello %s', name  
  27.  def add( self, a, b ):  
  28.   return a + b  
  29.     
  30. # test class   
  31. c1=cTest1()  
  32. c1.func1('my class')  
  33. res = c1.add(3,22)  
  34. print 'sum is %s' %res  
  35.    
  36. # for loop, and call funtions we defined above  
  37. for n in xrange( min, max ):  
  38.  str = 'now user with no %s login' %n  
  39.  MyPrint( str )  
  40.  UpdateUserID( n )  
  41.   
  42. </PRE><BR>  
  43. <PRE></PRE>  
  44. <P> </P>  
  45. <PRE></PRE>  
  46. <P> </P>  
  47. <PRE></PRE>  
  48. <P><STRONG>9.  全局变量,python中使用全局变量时必须加上global关键字,否则被视为局部变量或者未定义。</STRONG></P>  
  49. <PRE class=html name="code">g_user = 0  
  50.   
  51. def UseGlobalVar():  
  52.     global g_user  
  53.     if ( g_user == 0 ):  
  54.         #do something</PRE>   
  55. <P><BR>  
  56. <STRONG>10. 如果忘了给变量/对象赋值,直接使用这个对象的方法就会被告知,这个对象没有这个方法。</STRONG></P>  
  57. <P>这点很让我恶心,也是c/c++转过来的人很难适应的地方,在c/c++中,这个对象有没有这个方法主要看这个对象对应的类,有没有这个方法。为什么python不能提供像javascript的那种特性,既可以定义为无类型,也可以定义某一类型的变量。也正因为这样,python的类型可以几乎没有。</P>  
  58. <PRE class=html name="code">class classA:  
  59.     def func( self ):  
  60.         # do something here   
  61.   
  62. Obj_classA = None  
  63.   
  64. print'testing start' )  
  65. #Obj_classA = new ClassA()   
  66. Obj_class.func()</PRE>  
  67. <P> </P>  
  68. <P>其实如果说提示的是Obj_classA这个对象没有一个对应的类型,还是可以理解的。而不是没有这个方法。</P>  
  69. <P> </P>  
  70. <P><STRONG>11. python中变量可以赋任意类型的值<BR>  
  71. </STRONG></P>  
  72. <PRE class=html name="code"><STRONG>var = 12  
  73. var = 'hello'  
  74. var = [ 3222 ]  
  75. print ( var )</STRONG></PRE>  
  76. <P><BR>  
  77. 上面这些代码没有任何问题,最后打印出的是 [3 , 222 ].</P>  
  78. <P>其实这点我很不喜欢,弱类型或者无类型就有可能导致一些bug,而不容易被查出。</P>  
  79. <P> </P>  
  80. <P><STRONG>12. 求地址,c/c++通过&, 而python通过一个函数id函数</STRONG></P>  
  81. <PRE class=html name="code">a = 1  
  82. b = a  
  83. print( id( a ))  
  84. print( id( b ))</PRE>  
  85. <P><BR>  
  86.  </P>  
  87. <P>id()这个函数返回的是对象的全部唯一标识符,是整型或者长整型的数字,可以看做是对象的地址。</P>  
  88. <P> </P>  
  89. <P> </P>  
  90. <P><STRONG>13. 命名空间。c/c++是通过namespace关键字来定义和使用,而python中,这个东西没有用特别的关键字表示,而是一个python脚本文字自动被认为一个命名空间,而这个命名空间的名字就是文件的名字</STRONG>。</P>  
  91. <P>如我们有2个文件,一个定义函数和变量,另外一个则使用这些函数和变量。</P>  
  92. <PRE class=html name="code">#FuncAssembly.py  
  93.   
  94. count = 0  
  95.   
  96. def UserPass():  
  97.     global count  
  98.     count++  
  99.     return count  
  100.   
  101. def UserCancel():  
  102.     global count  
  103.     count--  
  104.     return count  
  105. </PRE>  
  106. <P><BR>  
  107.  </P>  
  108. <PRE class=html name="code">import FuncAssembly  
  109. #from FuncAssembly import *   
  110. #from FuncAssembly import UserPass   
  111.   
  112. print'start testing' )  
  113. res = FuncAessembly.UserPass()   
  114. print( res )</PRE>  
  115. <P><BR>  
  116.  </P>  
  117. <P>看到了吧,第二个文件要使用第一文件,只要import进来就可以了,import有点类似c/c++的#include。这个时候第一个文件的文件名就是它的命名空间,使用它的变量或者函数的时候,必须加上命名空间。</P>  
  118. <P> </P>  
  119. <P><STRONG>14. python的与或关系不是c/c++的 && 和 ||。而是直接使用自然语言 and 和 or</STRONG></P>  
  120. <P> </P>  
  121. <STRONG></STRONG><PRE class=html name="code">a = 1  
  122. b = 2  
  123. if (( a==1 ) and ( b == 2 )):  
  124.     print'all are correct' )  
  125.   
  126. if (( a == 1 ) or ( b == 3 )):  
  127.     print'some a is correct' )</PRE>  
  128. <P><BR>  
  129.  </P>  
  130. <P><STRONG>15. python中等于和
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值