标签:ror 机制 name sys ble erro python编程 c++ odi
在Python编程中经常会遇到函数(function),方法(method)及属性(attribute)以下划线‘_‘作为前缀,这里做个总结。
1 1. object # public 2 2. __object__ # special, python system use, user should not define like it 3 3. __object # private (name mangling during runtime) 4 4. _object # obey python coding convention, consider it as private
1 class Foo(): 2 def __init__(): 3 ... 4 def public_method(): 5 print ‘This is public method‘ 6 def __fullprivate_method(): 7 print ‘This is double underscore leading method‘ 8 def _halfprivate_method(): 9 print ‘This is one underscore leading method‘ 10 11 f = Foo() 12 f.public_method() # OK 13 f.__fullprivate_method() # Error occur 14 f._halfprivate_method() # OK
上文已经说明了,python中并没有真正意义的private,见以下方法便能够访问__fullprivate_method()
标签:ror 机制 name sys ble erro python编程 c++ odi
原文地址:http://www.cnblogs.com/chris-cp/p/6025934.html