处理不同类中的循环

时间:2018-03-29 17:59:47

标签: python python-3.x python-2.7 class while-loop

我有两个类,里面有一个打印数字计数的函数。在我使用的两个班级里面都是真的。

试用1: -

first.py -
class first:
    count = 0
    def firstm(self):
        while True:
            print("First Class Method : ",self.count)
second.py -
class second:
    count = 0
    def secondm(self):
        while True:
            print("Second Class Method : ",self.count)

 run.py -  
 from first import first
 from second import second

 class run:
     def runmethod(self):
        while True:
            first().firstm()
            second().secondm()

run().runmethod()

Output -
    First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
    First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
    First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5

Here it keep on repeating the first class method only.

在上面的代码中,程序仅针对第一类firstm方法运行。它不会转到下一堂课。

试验2: -

first.py -
class first:
    count = 0
    @classmethod
    def firstm(self):
        for i in range(0,10):
            print("First Class Method : ",self.count)

second.py -
class second:
    @classmethod
    def secondm(self):
        for i in range(0,10):
            print("Second Class Method : ",i)

run.py- 
from first import first
from second import second

class run:
    def runmethod(self):
        while True:
            first.firstm()
            second.secondm()

run().runmethod()

Output -
First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
Second Class : 0
Second Class : 1    
Second Class : 2
Second Class : 3
Second Class : 4
Second Class : 5
Second Class : 6
Second Class : 7
Second Class : 8
Second Class : 9
Third Class : 0
Third Class : 1
Third Class : 2
Third Class : 3
Third Class : 4
Third Class : 5
Third Class : 6
Third Class : 7
Third Class : 8
Third Class : 9
Fourth Class : 0
Fourth Class : 1
Fourth Class : 2
Fourth Class : 3
Fourth Class : 4
Fourth Class : 5
Fourth Class : 6
Fourth Class : 7
Fourth Class : 8
Fourth Class : 9
After this it won't print anything

在第二次试验中,它只运行一次代码然后停止。

试验3: -

first.py -
class first:
    count = 0
    def firstm(self):
        for i in range(0,10):
            print("First Class Method : ",self.count)

second.py -
class second:
    def secondm(self):
        for i in range(0,10):
            print("Second Class Method : ",i)

run.py-
from first import first
from second import second

class run:
    def runmethod(self):
        while True:
            first().firstm()
            second().secondm()

run().runmethod()

Output - 
First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
Second Class : 0
Second Class : 1    
Second Class : 2
Second Class : 3
Second Class : 4
Second Class : 5
Second Class : 6
Second Class : 7
Second Class : 8
Second Class : 9
Third Class : 0
Third Class : 1
Third Class : 2
Third Class : 3
Third Class : 4
Third Class : 5
Third Class : 6
Third Class : 7
Third Class : 8
Third Class : 9
Fourth Class : 0
Fourth Class : 1
Fourth Class : 2
Fourth Class : 3
Fourth Class : 4
Fourth Class : 5
Fourth Class : 6
Fourth Class : 7
Fourth Class : 8
Fourth Class : 9
    First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
Second Class : 0
Second Class : 1    
Second Class : 2
Second Class : 3
Second Class : 4
Second Class : 5
Second Class : 6
Second Class : 7
Second Class : 8
Second Class : 9
Third Class : 0
Third Class : 1
Third Class : 2
Third Class : 3
Third Class : 4
Third Class : 5
Third Class : 6
Third Class : 7
Third Class : 8
Third Class : 9
Fourth Class : 0
Fourth Class : 1
Fourth Class : 2
Fourth Class : 3
Fourth Class : 4
Fourth Class : 5
Fourth Class : 6
Fourth Class : 7
Fourth Class : 8
Fourth Class : 9
First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
Second Class : 0
Second Class : 1    
Second Class : 2
Second Class : 3
Second Class : 4
Second Class : 5
Second Class : 6
Second Class : 7
Second Class : 8
Second Class : 9
Third Class : 0
Third Class : 1
Third Class : 2
Third Class : 3
Third Class : 4
Third Class : 5
Third Class : 6
Third Class : 7
Third Class : 8
Third Class : 9
Fourth Class : 0
Fourth Class : 1
Fourth Class : 2
Fourth Class : 3
Fourth Class : 4
Fourth Class : 5
Fourth Class : 6
Fourth Class : 7
Fourth Class : 8
Fourth Class : 9
First Class : 0
First Class : 1
First Class : 2
First Class : 3
First Class : 4
First Class : 5
First Class : 6
First Class : 7
First Class : 8
First Class : 9
Second Class : 0
Second Class : 1    
Second Class : 2
Second Class : 3

在第三次试验中,我正确地输出了输出,但它创建了两个类的许多对象。

我想要做的是一旦run()。runmethod()首先运行它,然后执行firstm方法。 然后它转到第二个类并执行secondm方法。在完成secondm方法后,它再次转到第一类firstm方法并执行。

请告诉我如何处理它,或者有更好的方法。

3 个答案:

答案 0 :(得分:0)

对第三个代码块执行此操作。它应该能得到你想要的东西。

class run:
    def runmethod(self):
        for i in range(2):
            first().firstm()
            if i == 1:
                break
            second().secondm()

答案 1 :(得分:0)

first.firstm()始终打印0,因为您永远不会增加self.counter。除此之外,您的试用版2代码应按预期工作。

#first.py -
class first:
    count = 0
    @classmethod
    def firstm(self):
        self.count = 0
        for i in range(0,10):
            print("First Class Method : ",self.count)
            self.count += 1

#second.py -
class second:
    @classmethod
    def secondm(self):
        for i in range(0,10):
            print("Second Class Method : ",i)

#run.py
from first import first
from second import second

class run:
    def runmethod(self):
        while True:
            first.firstm()
            second.secondm()

run().runmethod()

DEMO

在演示中,我将while True:更改为for _ in range(0, 3):,因此演示会很快结束。

如果你想使用对象方法而不是类方法(试验3),但不希望每次循环都创建一个新对象,你可以通过在循环之前创建对象来实现。

class run:
    def runmethod(self):
        f = first()
        s = second()
        while True:
            f.firstm()
            s.secondm()

答案 2 :(得分:0)

在Trial-1中,我们可以使用count变量来控制while循环运行的次数。 我们可以递增每个类的count变量并仅运行该类,直到某个条件为True。 一旦条件为假,我们就可以退出课堂。

first.py -
class first:
    count = 0
    def firstm(self):
        while self.count<10:
            print("First Class Method : ",self.count)
            self.count = self.count+1
second.py -
class second:
    count = 0
    def secondm(self):
        while self.count<10:
            print("Second Class Method : ",self.count)
            self.count = self.count+1

run.py -  
from first import first
from second import second

 class run:
     def runmethod(self):
        while True:
            first().firstm()
            second().secondm()

run().runmethod()