Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:07:45+00:00 2026-06-09T01:07:45+00:00

I am having some problem accessing class instances. I am calling the class from

  • 0

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"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:07:47+00:00Added an answer on June 9, 2026 at 1:07 am

    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"])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problem accessing Rails.root from my rails engine, that I'm creating. I
I'm having some problems accessing the ID attribute of an ActiveRecord model. I want
I'm having some problem consuming REST Service and want to figure out what I'm
I'm trying to designing a class and I'm having issues with accessing some of
I'm having a problem accessing my javascripts from certain pages in my local Ruby
I'm having a problem accessing a class after deploying my website to a server.
I am having problem in accessing user control properties from page. I have usercontrol
I'm having some problem with validations for multiple fields, specifically with case-sensitive unique validations
The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
i am having some problem with position: absolute div. IE7 displays it next to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.