Error accessing class objects in python

2019-09-15 10:09发布

I am having some problem accessing class instances. I am calling the class from a procedure, name of instance is defined in some variable. I want the instance name to be created of that value and then want to access it, but when i access it is giving error. Can some one please help to resolve this issue.

class myclass:
  def __init__(self,object):
    self.name = object

def mydef():
   global a1
   b = "a1"
   b = myclass(b)

mydef()
print a1.name

Second Problem: In my actual script, I have to create a large number of such instances from this function (around 100). So defining their name as global would be painful, is there a way i could access those instances outside function without having to declare them as global.

Modification:

class myclass:
    def __init__(self,object,typename):
        self.name = object
        self.typeid = typename

def mydef():
    file_han = open(file,"r")
    while True:
      line = file_han.readline()
      if not line:
        break
      start = line.find('"')
      end = line.find('"',start+1)
      string_f = line[start+1:end]
      myclass(string_f,'a11')        

mydef(file)

print def.name
print def.typeid

File Contents are :
a11 "def"
a11 "ghi"
a11 "eff"

2条回答
疯言疯语
2楼-- · 2019-09-15 10:59

Here's how I'd do it. I don't know why you're messing around with globals, if you'd care to explain, I'll update my answer.

class Myclass(object):
    def __init__(self, name):
        self.name = name

def mydef():
   return Myclass("a1")

a1 = mydef()
print a1.name

Gather your instances in a list:

instances = []
for x in range(1000):
    instances.append(Myclass("Instance {0}".format(x)))
print instance[42].name

Note the changes:

  • Class names should be capitalized
  • Use object as the base class of your classes (since python 2.2, but no longer necessary in 3.x)
  • Don't shadow the built-in object with your parameter name
  • Just use the string "a1" directly as a parameter instead of assigning it to a variable
  • Return something from the function instead of passing the result by global variable

RE: Comment

You haven't said anything about the format of these files, so I'll just give an example where the file to be read contains one class name per line, and nothing else:

def mydef(filename):
    ret = []
    with open(filename) as f:
        for line in f:
            # Call `strip` on line to remove newline and surrounding whitespace
            ret.append(Myclass(line.strip()))
    return ret

So if you have several files and wish to add all your instances from all your files to a large list, do it like this:

instances = []
for filename in ["myfile1", "myfile2", "myfile3"]:
    instances.extend(mydef(filename))

RE: OP Edit

def mydef(filename):
    ret = []
    with open(filename, "r") as file_han:
        for line in file_han:
            string_f = line.split('"')[1]
            ret.append(Myclass(string_f))
    return ret

i = mydef("name_of_file")

RE: Comment

Oh, you want to access them by name. Then return a dict instead:

def mydef(filename):
    ret = {}
    with open(filename, "r") as file_han:
        for line in file_han:
            string_f = line.split('"')[1]
            ret[string_f] = Myclass(string_f)
    return ret

i = mydef("name_of_file")
print i["ghi"].name  # should print "ghi"

RE: Comment

If I understand you correctly, you want to have it both ways -- index by both line number and name. Well then why don't you return both a list and a dictionary?

def mydef(filename):
    d = {}
    L = []
    with open(filename, "r") as file_han:
        for line in file_han:
            string_f = line.split('"')[1]
            instance = Myclass(string_f)
            d[string_f] = instance
            L.append(instance)
    return L, d

L, d = mydef("name_of_file")
print d["ghi"].name
print L[3]
print L.index(d["ghi"])
查看更多
我命由我不由天
3楼-- · 2019-09-15 11:01

You could use class as repository for your instances, for example

class Named(object):
    def __init__(self,name):
        self.name = name

    def __new__(cls,name):
        instance = super(type,cls).__new__(cls,name)
        setattr(cls,name,instance)
        return instance

    def __repr__(self):
        return 'Named[%s]'%self.name

Named('hello')
Named('x123')
Named('this is not valid attribute name, but also working')

print(Named.hello,Named.x123,getattr(Named,'this is not valid attribute name, but also working'))
查看更多
登录 后发表回答