473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

insert python script in current script

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

Jun 27 '08 #1
3 1362
On 16 avr, 09:42, "Prashant" <shikha_saxena2...@yahoo.comwrote:
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
Jun 27 '08 #2
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
Jun 27 '08 #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 (ni**@stinemates.org)
http://nick.stinemates.org
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tero Pihlajakoski | last post by:
Hi, I've been experimenting on embedding Python to a C software, and ran into a little problem with PYTHONPATH (I'm running on linux). Here's the deal: When trying to call...
33
by: Darren Dale | last post by:
I love the language. I love the community. My only complaint is that Python for Windows is built with Visual Studio. It is too difficult to build python, or a module, from source. This is what...
2
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
20
by: Shawn Milo | last post by:
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The...
4
by: durumdara | last post by:
Hi! My problem is that: The "test.py" is working in every machine that I use except in my home machine. Formerly it worked but now not. I don't know what happened, I searching for some...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.