python类定义内部调用对象的实例

来源:互联网 发布:南昌大学教学网络平台 编辑:程序博客网 时间:2024/05/17 02:41

python类定义内部调用对象的实例 

#coding:utf-8

 
class Newclass:
    def __init__(self,intval = 1):
        self.the_int = intval
        if  intval % 2 == 0:
            self.parity = 'even'
        else :
            self.parity = 'odd'
 
    def process(self,instance):
        thesum = self.the_int + instance.the_int
        if   thesum < 0 :
            return 'nagative'
 
        elif  thesum % 2 == 0:
            return 'even'
        else :
            return 'odd'
    def __str__(self):
        return "value %d is %s" % (self.the_int,self.parity)
 
inst1 = Newclass(4)
inst2 = Newclass(-5)
inst3 = Newclass()
#print inst1
#print inst1.process(inst3)
print inst3.process(inst1)
print inst3