Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > insert python script in current script

Reply
Thread Tools

insert python script in current script

 
 
Prashant
Guest
Posts: n/a
 
      04-16-2008
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

 
Reply With Quote
 
 
 
 
colas.francis@gmail.com
Guest
Posts: n/a
 
      04-16-2008
On 16 avr, 09:42, "Prashant" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Nick Stinemates
Guest
Posts: n/a
 
      04-18-2008
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 ((E-Mail Removed))
http://nick.stinemates.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the current method name within current method Matthew Heidemann Ruby 14 03-22-2011 03:56 AM
printing out the current URL for current ASP page in Perl Jack Perl Misc 3 12-22-2008 02:35 PM
Thread.current.parent and Thread.current.ancestors ara.t.howard@noaa.gov Ruby 3 02-16-2007 03:34 PM
How to update the current web page? (delete current web page + rewrite the page) laryten@yahoo.com Javascript 3 10-12-2006 07:33 PM
How to change the current thread current culture at run time. Manu ASP .Net 1 07-20-2003 03:27 PM



Advertisments