Python 递归调用self,求助!

Quintus 发布于 2012/08/08 13:51
阅读 2K+
收藏 0

代码如下,

 

问题出在toString方法,假如对象AchildTags,然后调用toString时候就循环调用childTags的每个对象的toString方法,但是我Debug看到传给toString方法的self对象一直看起来都是最初的A对象,请问代码该怎么写,问题出在哪里,初学python,谢谢了。

class Tag(object):
    attributes = {}
    html = ''
    childTags = []
    
    '''__init(self,name, attributes, HTML)'''
    def __init__(self, name, *args):
        self.name = name
        if len(args) < 1 :
            return
        if len(args) == 1 and isinstance(args[0], dict) :
            self.attributes = args[0] 
            return 
        elif len(args) == 1 and isinstance(args[0], str):
            self.html = args[0]
            return
        if len(args) == 2:
            if isinstance(args[0], dict) and isinstance(args[1], str):
                self.attributes = args[0]
                self.html = args[1]
            elif isinstance(args[1], dict) and isinstance(args[0], str):   
                self.attributes = args[1]
                self.html = args[0]
            return
        else:
            raise Exception, 'Illegal arguments'
                
    def toString(self):
        if len(self.childTags) == 0:
            tmp = ' '
            for attribute in self.attributes.keys():
                tmp = tmp + attribute + '=\'' + self.attributes[attribute] + '\' ' 
                tag = '<' + self.name + tmp + '>' + self.html + '</' + self.name + '>'
        else:
            tmpAttr = ' '
            tmpChildren = ''
            
            for child in self.childTags:
                tmpChildren = tmpChildren + child.toString() + '\n'
            
            for attribute in self.attributes.keys():
                tmpAttr = tmpAttr + attribute + '=\'' + self.attributes[attribute] + '\' '  
                tag = '<' + self.name + tmpAttr + '>' + self.html + tmpChildren + '</' + self.name + '>'
        return tag
    
    def __str__(self):
        return self.toString()
    
    def setINNERHTML(self, html):
        if html == '':
            self.html = ''
        else:
            self.html = html
    def setAttributes(self, attributes):
        if isinstance(attributes, dict):
            self.attributes = attributes
        else:
            raise Exception, 'Attributes of tag must be a dictionary'
 
    def contains(self, *childTags):
        for tag in childTags:
            if isinstance(tag, Tag):
                self.childTags.append(tag)
            else:
                pass


加载中
0
ChenQi
ChenQi
class Tag(object):
#    attributes = {}                                                                                                                                                                                            
#    html = ''                                                                                                                                                                                                  
#    childTags = []                                                                                                                                                                                             

    '''__init(self,name, attributes, HTML)'''
    def __init__(self, name, *args):
        self.attributes = {}
        self.html = ''
        self.childTags = []
        self.name = name
        if len(args) < 1 :

Quintus
Quintus
方法参数我就是设置的可变化的,我的意思是递归到child的时候调用tostring方法self对象还是A对象,我对这个不解
0
Yisen
Yisen

忍不出插一句

'''的注释是写在函数下方的

ChenQi
ChenQi
恩。好像类里的method的话,就写在前面了。
0
Quintus
Quintus

引用来自“ChenQi”的答案

class Tag(object):
#    attributes = {}                                                                                                                                                                                            
#    html = ''                                                                                                                                                                                                  
#    childTags = []                                                                                                                                                                                             

    '''__init(self,name, attributes, HTML)'''
    def __init__(self, name, *args):
        self.attributes = {}
        self.html = ''
        self.childTags = []
        self.name = name
        if len(args) < 1 :

我的意思就是要不定参数的长度
Quintus
Quintus
类就是 class的定义, object就是class的实例,是吧?还有定义的class继承自object和直接定义class有什么区别啊,python中的object具体是拿来干嘛的呢?
ChenQi
ChenQi
注意类和对象的区别。
0
zhengyijie
zhengyijie
应该是引用和拷贝的问题,我想应该是对对象A的引用,而不是拷贝。
Quintus
Quintus
你好,你的意思是self关键字一直都是A对象的拷贝,所以调用childTag的tostring方法里面的self关键字还是A对象,所以出现了错误,是这个意思吗? 那么我该如何调用childTag的tostring方法呢,请仔细讲解一下,谢谢
0
ChenQi
ChenQi
class MemberCounter:
members = 0
def init(self):
MemberCounter.members += 1
>>>
>>>
>>>
1
>>>
>>>
>>>
2
m1 = MemberCounter()
m1.init()
MemberCounter.members
m2 = MemberCounter()
m2.init()
MemberCounter.members
In the preceding code, a variable is defined in the class scope, which can be accessed by all
the members (instances), in this case to count the number of class members. Note the use of
init to initialize all the instances: I’ll automate that (that is, turn it into a proper constructor)
in Chapter 9.
This class scope variable is accessible from every instance as well, just as methods are:
>>> m1.members
2
>>> m2.members
2
What happens when you rebind the members attribute in an instance?
>>> m1.members = 'Two'
>>> m1.members
'Two'
>>> m2.members
2
The new members value has been written into an attribute in m1, shadowing the class-wide
variable.

0
Quintus
Quintus

引用来自“ChenQi”的答案

class MemberCounter:
members = 0
def init(self):
MemberCounter.members += 1
>>>
>>>
>>>
1
>>>
>>>
>>>
2
m1 = MemberCounter()
m1.init()
MemberCounter.members
m2 = MemberCounter()
m2.init()
MemberCounter.members
In the preceding code, a variable is defined in the class scope, which can be accessed by all
the members (instances), in this case to count the number of class members. Note the use of
init to initialize all the instances: I’ll automate that (that is, turn it into a proper constructor)
in Chapter 9.
This class scope variable is accessible from every instance as well, just as methods are:
>>> m1.members
2
>>> m2.members
2
What happens when you rebind the members attribute in an instance?
>>> m1.members = 'Two'
>>> m1.members
'Two'
>>> m2.members
2
The new members value has been written into an attribute in m1, shadowing the class-wide
variable.

Thanks a lot, that's so helpful.
返回顶部
顶部