insert python script in current script

Discussion in 'Python' started by Prashant, Apr 16, 2008.

  1. Prashant

    Prashant Guest

    I was wondering is there any way to do this:

    I have written a class in python and __init__ goes like this:

    def __init__(self):

    self.name = 'jack'
    self.age = 50

    import data




    now here there is data.py in the same directory and contents are like:

    self.address = 'your address'
    self.status = 'single'

    The problem is 'self' is giving some error here. I need to know if
    somehow I can do this. It's like inserting the script as it's part of
    the file itself.

    Cheers
     
    Prashant, Apr 16, 2008
    #1
    1. Advertising

  2. Prashant

    Guest

    On 16 avr, 09:42, "Prashant" <> wrote:
    > I was wondering is there any way to do this:
    >
    > I have written a class in python and __init__ goes like this:
    >
    > def __init__(self):
    >
    > self.name = 'jack'
    > self.age = 50
    >
    > import data
    >
    > now here there is data.py in the same directory and contents are like:
    >
    > self.address = 'your address'
    > self.status = 'single'
    >
    > The problem is 'self' is giving some error here. I need to know if
    > somehow I can do this. It's like inserting the script as it's part of
    > the file itself.


    The purpose of import is to build a module object, which implies
    executing the module file but in a new context.
    If you simply want to execute some code in a file, you can try
    execfile("filename"):

    In [243]: class A(object):
    .....: def __init__(self):
    .....: execfile("test.py")
    .....:

    In [244]: a=A()

    In [245]: a.a
    Out[245]: 1

    In [246]: open("test.py").read()
    Out[246]: 'self.a = 1\n'

    But do you really want to execute some arbitrary code or to initialize
    values with some kind of configuration file?

    >
    > Cheers
     
    , Apr 16, 2008
    #2
    1. Advertising

  3. On Wed, Apr 16, 2008 at 01:41:13PM -0500, Larry Bates wrote:
    > Prashant wrote:
    > > I was wondering is there any way to do this:
    > >
    > > I have written a class in python and __init__ goes like this:
    > >
    > > def __init__(self):
    > >
    > > self.name = 'jack'
    > > self.age = 50
    > >
    > > import data
    > >
    > >
    > >
    > >
    > > now here there is data.py in the same directory and contents are like:
    > >
    > > self.address = 'your address'
    > > self.status = 'single'
    > >
    > > The problem is 'self' is giving some error here. I need to know if
    > > somehow I can do this. It's like inserting the script as it's part of
    > > the file itself.
    > >
    > > Cheers
    > >

    >
    > Can you give a use case for doing this. You would most likely be better doing:
    >
    > class person(object):
    > def __init__(self, name=None, age=None):
    > self.name=name
    > self.age=age
    >
    >
    > personInstance=person(name='jack', age='50)
    >
    > -Larry
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Could it also be that he would like to have a base class? Cause that's
    what It sounds like to me!

    class Base:
    def __init__(self):
    self.address = "address"
    self.status = 1 //use numbers instead of strings :)

    class Person(Base):
    def __init__(self):
    Base.__init__(self)
    # now you have the self.address, self.status


    --
    Nick Stinemates ()
    http://nick.stinemates.org
     
    Nick Stinemates, Apr 18, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manu
    Replies:
    1
    Views:
    6,140
    Natty Gur
    Jul 20, 2003
  2. Matthew Heidemann
    Replies:
    14
    Views:
    259
    eT Ma
    Mar 22, 2011
  3. Replies:
    3
    Views:
    184
  4. Jack
    Replies:
    3
    Views:
    634
    Tad J McClellan
    Dec 22, 2008
  5. Replies:
    3
    Views:
    408
Loading...

Share This Page