如何在不使用全局变量的情况下从Python中返回对象

时间:2022-08-24 16:15:29

I am trying to return the region from my __maketracker__ method in my Tracker class. Every time I run this code, region is identified as the global variable and not the one returned from my class. If I delete region="" (the global variable) then it gives me an error that says region is undefined. line is from an input file.

我试图从我的Tracker类中的__maketracker__方法返回该区域。每次运行此代码时,region都被标识为全局变量,而不是从我的类返回的变量。如果我删除region =“”(全局变量),那么它会给出一个错误,表示区域未定义。 line来自输入文件。

temp = ""
begining = ""
end = ""
flag = ""
region = ""

class Tracker(object):   

    def __init__(self,region):
        self.region = region

    def __maketracker__(self):
        self.region = re.search(r'CLI Command: \'show card detail\'',line)
        if self.region:
            self.region = "CPM OR IOM"
            region = self.region
            return region

with open ('/home/SAMPLE.TXT','r') as f:

    for line in f:    
        #This is where I am calling the class
        first = Tracker(line)
        first.__maketracker__()

        if region =="CPM or IOM":
            print "CPM or IOM"

3 个解决方案

#1


1  

first is the object you created, so one way to access the variable is directly as follows:

首先是您创建的对象,因此访问变量的一种方法如下:

if first.region == "CPM or IOM":
    print("CPM or IOM")

The problem with your program is that you don't do anything with the returned valued. It should be as follows:

您的程序的问题是您不对返回的值执行任何操作。它应该如下:

region_ = first.__maketracker__()
if region_ == "CPM or IOM":
    print ...

So, if we remove all globals and rewrite:

所以,如果我们删除所有全局变量并重写:

class Tracker(object):   

    def __init__(self, region):
        self.region = region

    def make_tracker(self):
        self.region = re.search(r'CLI Command: \'show card detail\'', line)
        if self.region:
            self.region = "CPM OR IOM"
            return self.region

with open ('/home/SAMPLE.TXT','r') as f:
    for line in f:    
        first = Tracker(line)
        region = first.make_tracker()
        if region =="CPM or IOM":
            print "CPM or IOM"

#2


0  

You are returning region in maketracker, so you have to assign it to a variable in the callee:

你在maketracker中返回区域,所以你必须将它分配给被调用者中的变量:

with open ('/home/SAMPLE.TXT','r') as f:
    for line in f:    
        #This is where I am calling the class
        first = Tracker(line)
        region = first.__maketracker__()

        if region == "CPM or IOM":
            print "CPM or IOM"

#3


0  

Hints:

提示:

Just eliminate the use of the globals. Use the object to store attributes.

只是消除了全局变量的使用。使用该对象存储属性。

Not tested, but something like:

没有经过测试,但是类似于:

class Tracker(object):   

    def __init__(self,region):
        self.region = region

    def maketracker(self, line):
        if re.search(r'CLI Command: \'show card detail\'',line):
            self.region = "CPM OR IOM"
            return self.region

#1


1  

first is the object you created, so one way to access the variable is directly as follows:

首先是您创建的对象,因此访问变量的一种方法如下:

if first.region == "CPM or IOM":
    print("CPM or IOM")

The problem with your program is that you don't do anything with the returned valued. It should be as follows:

您的程序的问题是您不对返回的值执行任何操作。它应该如下:

region_ = first.__maketracker__()
if region_ == "CPM or IOM":
    print ...

So, if we remove all globals and rewrite:

所以,如果我们删除所有全局变量并重写:

class Tracker(object):   

    def __init__(self, region):
        self.region = region

    def make_tracker(self):
        self.region = re.search(r'CLI Command: \'show card detail\'', line)
        if self.region:
            self.region = "CPM OR IOM"
            return self.region

with open ('/home/SAMPLE.TXT','r') as f:
    for line in f:    
        first = Tracker(line)
        region = first.make_tracker()
        if region =="CPM or IOM":
            print "CPM or IOM"

#2


0  

You are returning region in maketracker, so you have to assign it to a variable in the callee:

你在maketracker中返回区域,所以你必须将它分配给被调用者中的变量:

with open ('/home/SAMPLE.TXT','r') as f:
    for line in f:    
        #This is where I am calling the class
        first = Tracker(line)
        region = first.__maketracker__()

        if region == "CPM or IOM":
            print "CPM or IOM"

#3


0  

Hints:

提示:

Just eliminate the use of the globals. Use the object to store attributes.

只是消除了全局变量的使用。使用该对象存储属性。

Not tested, but something like:

没有经过测试,但是类似于:

class Tracker(object):   

    def __init__(self,region):
        self.region = region

    def maketracker(self, line):
        if re.search(r'CLI Command: \'show card detail\'',line):
            self.region = "CPM OR IOM"
            return self.region