在Python中定义类class的时候,可以加上():
class Employee():
"""这里是Employee的__doc__"""
empCount = 0
__name1 = 'safety'
def __init__(self,name,salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def display_count(self):
print('Totall employee count:{}'.format(Employee.empCount))
def display_employee(self):
print('Name:{}, Salary:{}'.format(self.name, self.salary))
也可以不加上():
class Employee:
"""这里是Employee的__doc__"""
empCount = 0
__name1 = 'safety'
def __init__(self,name,salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def display_count(self):
print('Totall employee count:{}'.format(Employee.empCount))
def display_employee(self):
print('Name:{}, Salary:{}'.format(self.name, self.salary))
目前我观察的结果是:二者没有区别;实际上它们有无区别呢?如果真的没有区别,那么哪种写法是正确的?我在《Python基础教程》中看到的是不加括号。
事实上是没有区别的,以下三种写法是等价的