Python';s类继承
实例化类时,不需要传入Python';s类继承,python,class,inheritance,Python,Class,Inheritance,实例化类时,不需要传入base\u class。这是在定义上完成的\uuuu init\uuuu只接受一个参数,即self,并且是自动的。你只需要打个电话 # Defining a Base class to be shared among many other classes later: class Base(dict): """Base is the base class from which all the class will derrive. """ nam
base\u class
。这是在定义上完成的<代码>\uuuu init\uuuu只接受一个参数,即self
,并且是自动的。你只需要打个电话
# Defining a Base class to be shared among many other classes later:
class Base(dict):
"""Base is the base class from which all the class will derrive.
"""
name = 'name'
def __init__( self):
"""Initialise Base Class
"""
dict.__init__(self)
self[Base.name] = ""
# I create an instance of the Base class:
my_base_instance = Base()
# Since a Base class inherited from a build in 'dict' the instance of the class is a dictionary. I can print it out with:
print my_base_instance Results to: {'name': ''}
# Now I am defining a Project class which should inherit from an instance of Base class:
class Project(object):
def __init__(self):
print "OK"
self['id'] = ''
# Trying to create an instance of Project class and getting the error:
project_class = Project(base_class)
TypeError: __init__() takes exactly 1 argument (2 given)
实例化类时,不需要传入
base\u class
。这是在定义上完成的<代码>\uuuu init\uuuu只接受一个参数,即self
,并且是自动的。你只需要打个电话
# Defining a Base class to be shared among many other classes later:
class Base(dict):
"""Base is the base class from which all the class will derrive.
"""
name = 'name'
def __init__( self):
"""Initialise Base Class
"""
dict.__init__(self)
self[Base.name] = ""
# I create an instance of the Base class:
my_base_instance = Base()
# Since a Base class inherited from a build in 'dict' the instance of the class is a dictionary. I can print it out with:
print my_base_instance Results to: {'name': ''}
# Now I am defining a Project class which should inherit from an instance of Base class:
class Project(object):
def __init__(self):
print "OK"
self['id'] = ''
# Trying to create an instance of Project class and getting the error:
project_class = Project(base_class)
TypeError: __init__() takes exactly 1 argument (2 given)
实例化类时,不需要传入
base\u class
。这是在定义上完成的<代码>\uuuu init\uuuu只接受一个参数,即self
,并且是自动的。你只需要打个电话
# Defining a Base class to be shared among many other classes later:
class Base(dict):
"""Base is the base class from which all the class will derrive.
"""
name = 'name'
def __init__( self):
"""Initialise Base Class
"""
dict.__init__(self)
self[Base.name] = ""
# I create an instance of the Base class:
my_base_instance = Base()
# Since a Base class inherited from a build in 'dict' the instance of the class is a dictionary. I can print it out with:
print my_base_instance Results to: {'name': ''}
# Now I am defining a Project class which should inherit from an instance of Base class:
class Project(object):
def __init__(self):
print "OK"
self['id'] = ''
# Trying to create an instance of Project class and getting the error:
project_class = Project(base_class)
TypeError: __init__() takes exactly 1 argument (2 given)
实例化类时,不需要传入
base\u class
。这是在定义上完成的<代码>\uuuu init\uuuu只接受一个参数,即self
,并且是自动的。你只需要打个电话
# Defining a Base class to be shared among many other classes later:
class Base(dict):
"""Base is the base class from which all the class will derrive.
"""
name = 'name'
def __init__( self):
"""Initialise Base Class
"""
dict.__init__(self)
self[Base.name] = ""
# I create an instance of the Base class:
my_base_instance = Base()
# Since a Base class inherited from a build in 'dict' the instance of the class is a dictionary. I can print it out with:
print my_base_instance Results to: {'name': ''}
# Now I am defining a Project class which should inherit from an instance of Base class:
class Project(object):
def __init__(self):
print "OK"
self['id'] = ''
# Trying to create an instance of Project class and getting the error:
project_class = Project(base_class)
TypeError: __init__() takes exactly 1 argument (2 given)
对于要从基继承的项目,您不应该从对象中将其子类化,而应该从基继承,即
类项目(基)
。您将得到TypeError:init()在实例化项目类时正好接受1个参数(给定2个)
错误,因为构造函数只接受1个参数(self
),并且您也传递了base\u class
)<代码>'self'由python隐式传递。对于要从基继承的项目,您不应该从对象中将其子类化,而应该从基继承,即将项目(基)
分类。您将得到TypeError:init()在实例化项目类时正好接受1个参数(给定2个)
错误,因为构造函数只接受1个参数(self
),并且您也传递了base\u class
)<代码>'self'由python隐式传递。对于要从基继承的项目,您不应该从对象中将其子类化,而应该从基继承,即将项目(基)
分类。您将得到TypeError:init()在实例化项目类时正好接受1个参数(给定2个)
错误,因为构造函数只接受1个参数(self
),并且您也传递了base\u class
)<代码>'self'由python隐式传递。对于要从基继承的项目,您不应该从对象中将其子类化,而应该从基继承,即将项目(基)
分类。您将得到TypeError:init()在实例化项目类时正好接受1个参数(给定2个)
错误,因为构造函数只接受1个参数(self
),并且您也传递了base\u class
)<代码>'self'由python隐式传递。代码中有两个错误:
1) 类继承
project_class = Project()
2) 实例定义(您的\uuuuu init\uuuu
不需要任何显式参数,当然也不需要祖先类)
代码中有两个错误: 1) 类继承
project_class = Project()
2) 实例定义(您的\uuuuu init\uuuu
不需要任何显式参数,当然也不需要祖先类)
代码中有两个错误: 1) 类继承
project_class = Project()
2) 实例定义(您的\uuuuu init\uuuu
不需要任何显式参数,当然也不需要祖先类)
代码中有两个错误: 1) 类继承
project_class = Project()
2) 实例定义(您的\uuuuu init\uuuu
不需要任何显式参数,当然也不需要祖先类)
定义类时必须声明继承性(
class-Project(Base)
而不是实例化它。定义类时必须声明继承性(class-Project(Base)
而不是实例化它。定义类时必须声明继承性(class-Project(Base)
而不是在实例化它时。定义类时必须声明继承性(类项目(基)
而不是在实例化它时。