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 7755229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:30:58+00:00 2026-06-01T12:30:58+00:00

Code is much more precise than English; Here’s what I’d like to do: import

  • 0

Code is much more precise than English; Here’s what I’d like to do:

import sys

fileName = sys.argv[1]
className = sys.argv[2]

# open py file here and import the class
# ???

# Instantiante new object of type "className"
a = eval(className + "()") # I don't know if this is the way to do that.

# I "know" that className will have this method:
a.writeByte(0x0)

EDIT:

Per the request of the answers, here’s what I’m trying to do:

I’m writing a virtual processor adhering to the SIC/XE instruction set. It’s an educational theoretical processor used to teach the fundamentals of assembly language and systems software to computer science students. There is a notion of a “device” that I’m trying to abstract from the programming of the “processor.” Essentially, I want the user of my program to be able to write their own device plugin (limited to “read_byte” and “write_byte” functionality) and then I want them to be able to “hook up” their devices to the processor at command-line time, so that they can write something like:

python3 sicsim -d1 dev1module Dev1Class -d2 ...

They would also supply the memory image, which would know how to interact with their device. I basically want both of us to be able to write our code without it interfering with each other.

  • 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-01T12:31:00+00:00Added an answer on June 1, 2026 at 12:31 pm

    Use importlib.import_module and the built in function getattr. No need for eval.

    import sys
    import importlib
    
    module_name = sys.argv[1]
    class_name = sys.argv[2]
    
    module = importlib.import_module(module_name)
    cls = getattr(module, class_name)
    
    obj = cls()
    obj.writeByte(0x0)
    

    This will require that the file lives somewhere on your python path. Most of the time, the current directory is on said path. If this is not sufficient, you’ll have to parse the directory out of it and append it to the sys.path. I’ll be glad to help with that. Just give me a sample input for the first commandline argument.

    Valid input for this version would be something like:

    python3 myscript.py mypackage.mymodule MyClass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another unimportant performance question. Unimportant because mostly code-readability is much more important than a
I would like to abstract out pure SQL calls in my code as much
I have code blindness, it's like snowblindness, just wth far too much code. I
I sought for a solution to make a Ruby code much more unreadable. In
Here is what I would like to write in my java code: private <A
not much more than the title says. If I do: int[] arr = new
Edit : Here is a much more simpler example of this issue (i've deleted
jQuery plugins make code much more reusable. I previously made a plugin that would
for element in container: # some code here temp_variable = f1(element) # more code
How much code kludge could you remove using the resharper 4? I want 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.