下面的资料,会随着学习的不断深入,持续的更新。
内容没有能有效的组织,因为没一点是本人再学习的过程中,慢慢的总结出来的。等本篇文字足够长的时候,可能会考虑组织文件可读性更强的文字。
1. c/c++里面,注释代码是用 // ,而python是用 #
2. c/c++里面,定义类对象
- // define class
- class ClassA
- {
- ...
- };
- //
- ClassA OA; // 对象
- ClassA *pA = new ClassA; // 指针
// define class
class ClassA
{
...
};
//
ClassA OA; // 对象
ClassA *pA = new ClassA; // 指针
而python里面
- # define class
- class ClassA:
- def func( self, name ):
- #do something
- # declare a object of the class
- ca = ClassA()
- 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的类继承。
- class ClassA:
- def funca( self, name ):
- #do something
- class ClassB(ClassA):
- def funcb( self, name ):
- #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则是用单引号 '', 或者是双引号""
- char *p = "hello";
- 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解释器去执行该脚本。
- <PRE class=python name="code"># test simple print
- print 'hello world'
- name = 'myname'
- print 'hello %s' %name
- # define variables
- max=10000
- min=1
- useridbase='2012FFD_'
- # define a function to print string
- def MyPrint( str ):
- print str
- # define a function to do somthing
- def UpdateUserID( id ):
- idd = id + 1
- idCur = '%s' %idd
- useridCurrent = useridbase+idCur
- print 'user name: %s' %useridCurrent
- return id+1
- # define class
- class cTest1:
- def func1( self, name ):
- print 'hello %s', name
- def add( self, a, b ):
- return a + b
- # test class
- c1=cTest1()
- c1.func1('my class')
- res = c1.add(3,22)
- print 'sum is %s' %res
- # for loop, and call funtions we defined above
- for n in xrange( min, max ):
- str = 'now user with no %s login' %n
- MyPrint( str )
- UpdateUserID( n )
- </PRE><BR>
- <PRE></PRE>
- <P> </P>
- <PRE></PRE>
- <P> </P>
- <PRE></PRE>
- <P><STRONG>9. 全局变量,python中使用全局变量时必须加上global关键字,否则被视为局部变量或者未定义。</STRONG></P>
- <PRE class=html name="code">g_user = 0
- def UseGlobalVar():
- global g_user
- if ( g_user == 0 ):
- #do something</PRE>
- <P><BR>
- <STRONG>10. 如果忘了给变量/对象赋值,直接使用这个对象的方法就会被告知,这个对象没有这个方法。</STRONG></P>
- <P>这点很让我恶心,也是c/c++转过来的人很难适应的地方,在c/c++中,这个对象有没有这个方法主要看这个对象对应的类,有没有这个方法。为什么python不能提供像javascript的那种特性,既可以定义为无类型,也可以定义某一类型的变量。也正因为这样,python的类型可以几乎没有。</P>
- <PRE class=html name="code">class classA:
- def func( self ):
- # do something here
- Obj_classA = None
- print( 'testing start' )
- #Obj_classA = new ClassA()
- Obj_class.func()</PRE>
- <P> </P>
- <P>其实如果说提示的是Obj_classA这个对象没有一个对应的类型,还是可以理解的。而不是没有这个方法。</P>
- <P> </P>
- <P><STRONG>11. python中变量可以赋任意类型的值<BR>
- </STRONG></P>
- <PRE class=html name="code"><STRONG>var = 12
- var = 'hello'
- var = [ 3, 2, 22 ]
- print ( var )</STRONG></PRE>
- <P><BR>
- 上面这些代码没有任何问题,最后打印出的是 [3 , 2, 22 ].</P>
- <P>其实这点我很不喜欢,弱类型或者无类型就有可能导致一些bug,而不容易被查出。</P>
- <P> </P>
- <P><STRONG>12. 求地址,c/c++通过&, 而python通过一个函数id函数</STRONG></P>
- <PRE class=html name="code">a = 1
- b = a
- print( id( a ))
- print( id( b ))</PRE>
- <P><BR>
- </P>
- <P>id()这个函数返回的是对象的全部唯一标识符,是整型或者长整型的数字,可以看做是对象的地址。</P>
- <P> </P>
- <P> </P>
- <P><STRONG>13. 命名空间。c/c++是通过namespace关键字来定义和使用,而python中,这个东西没有用特别的关键字表示,而是一个python脚本文字自动被认为一个命名空间,而这个命名空间的名字就是文件的名字</STRONG>。</P>
- <P>如我们有2个文件,一个定义函数和变量,另外一个则使用这些函数和变量。</P>
- <PRE class=html name="code">#FuncAssembly.py
- count = 0
- def UserPass():
- global count
- count++
- return count
- def UserCancel():
- global count
- count--
- return count
- </PRE>
- <P><BR>
- </P>
- <PRE class=html name="code">import FuncAssembly
- #from FuncAssembly import *
- #from FuncAssembly import UserPass
- print( 'start testing' )
- res = FuncAessembly.UserPass()
- print( res )</PRE>
- <P><BR>
- </P>
- <P>看到了吧,第二个文件要使用第一文件,只要import进来就可以了,import有点类似c/c++的#include。这个时候第一个文件的文件名就是它的命名空间,使用它的变量或者函数的时候,必须加上命名空间。</P>
- <P> </P>
- <P><STRONG>14. python的与或关系不是c/c++的 && 和 ||。而是直接使用自然语言 and 和 or</STRONG></P>
- <P> </P>
- <STRONG></STRONG><PRE class=html name="code">a = 1
- b = 2
- if (( a==1 ) and ( b == 2 )):
- print( 'all are correct' )
- if (( a == 1 ) or ( b == 3 )):
- print( 'some a is correct' )</PRE>
- <P><BR>
- </P>
- <P><STRONG>15. python中等于和