时间是一切财富中最宝贵的财富。--------德奥弗拉斯多
Python中关键字变量和特殊函数,都是以__xxx__来表示的,初学Python的朋友,需要注意其中变量名中前后是有两个下划线(_)的,如果不注意,调用内部关键字变量和特殊函数时,将会出现错误:
<模块调用时>
'A test module'
import sys
def test():
args = sys.argv
if len(args)==1:
print('Hello,World!')
elif len(args)==2:
print('Hello, %s!' % args[1])
else:
print('Too many arguments!')
if _name_=='_main_':
test()
"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/_first_module.py"
Traceback (most recent call last):
File "E:/python/python project/myfirst/vari/_first_module.py", line 17, in
if _name_=='_main_':
NameError: name '_name_' is not defined
<面向对象编程时>
class Student(object):
def _init_(self,name,age):
self.name=name
self.age=age
def print_core(self):
print('%s:%s' % (self.name,self.age))
stu1 = Student('tpc',26)
stu1.print_core()
"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/_first_class.py"
Traceback (most recent call last):
File "E:/python/python project/myfirst/vari/_first_class.py", line 10, in
stu1 = Student('tpc',26)
TypeError: object() takes no parameters
class Student(object):
def __init__(self,name,age):
self.name=name
self.age=age
def print_core(self):
print('%s:%s' % (self.name,self.age))
stu1 = Student('tpc',26)
stu1.print_core()
"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/_first_class.py"
tpc:26
喜欢的朋友可以扫描以下二维码进行关注,公众号将每天更新文章: