Giter Site home page Giter Site logo

Comments (3)

dbieber avatar dbieber commented on May 19, 2024

Try this command instead
Without modifying your code, you should be able to run this command:

python a.py B --arg1 arg1 --arg2 arg2 _imp create 3

What was wrong with your command?
Here's what is happening now when you run python a.py B --arg1 arg1 --arg2 arg2 create 3:

python a.py: runs the a.py file, where fire.Fire() is called. Since you pass no arguments to Fire, it makes everything in a.py available at the command line. I think you're importing B in a.py, so B is available.

B --arg1 arg1 --arg2 arg2: An instance of B is constructed with arguments arg1 and arg2.

create: Fire then tries to use the create argument to access a member of your instance of B, which fails since B doesn't have a method named create. If instead you access the _imp member of B, and then call create, it should do what you're looking for.

from python-fire.

venkatraman07 avatar venkatraman07 commented on May 19, 2024

Thanks for explanation. Follow up question. if I initialize another class X and add a method in a.py like this

class A (object):
    def __init__(self, arg1, arg2):
         self.x  = X()   <----- added new initialization
         self.b = B(arg1, arg2)
         self.arg1 = arg1
         self.arg2 = arg2

     def connect(self)   <---- added new method
          some implementation code...

x.py

class X():   <-- how can I access or pass the arg1 arg2 .. to X()? So that the methodsin X can access those arguments
     def create()
           print("x");

if I run the following command
python a.py A --arg1 arg1 --arg2 arg2 connect
connect will have access to all the arguments that were passed to A. Is there any way we could pass these arguments to class X?

from python-fire.

dbieber avatar dbieber commented on May 19, 2024

If I understand your question correctly, then one option is to pass them via X's constructor, just as you do for B:

class A (object):
    def __init__(self, arg1, arg2):
         self.x = X(arg1, arg2)   # added arg1 and arg2 here
         self.b = B(arg1, arg2)
         self.arg1 = arg1
         self.arg2 = arg2

     def connect(self):
          some implementation code...
class X():   
     def __init__(self, arg1, arg2):  # methods in X can now access the arguments from A
       self.arg1 = arg1
       self.arg2 = arg2
     def create():
           print("x", self.arg1, self.arg2);

Or if you want X to have even broader access to A's properties, you could pass an instance of A itself, like this:

class A (object):
    def __init__(self, arg1, arg2):
         self.x = X(self)   # we give X access to an instance of A
         self.b = B(arg1, arg2)
         self.arg1 = arg1
         self.arg2 = arg2

     def connect(self):
          some implementation code...
class X():
     def __init__(self, a):
       self.a = a   # X can now access anything from a
     def create():
           print("x", self.a.arg1, self.a.arg2);   # Here's how you'd access arg1 and arg2

from python-fire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.