473,387 Members | 1,711 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,387 software developers and data experts.

Access to variable from external imported module

How to access to a variable (that value is not returned) from a module
imported?
And the variable is set at the module-level.

That module is external to my program, it's from another project so I
wann't modifying it.

Nov 23 '06 #1
17 1659
GinTon wrote:
How to access to a variable (that value is not returned) from a module
imported?
And the variable is set at the module-level.
import module
print module.variable

(have you read the Python tutorial?)

</F>

Nov 23 '06 #2
Sorry, I mean access to local variable from a method

import module
method(value)

I would to access to values that are created locally in that method

Fredrik Lundh ha escrito:
GinTon wrote:
How to access to a variable (that value is not returned) from a module
imported?
And the variable is set at the module-level.

import module
print module.variable

(have you read the Python tutorial?)

</F>
Nov 23 '06 #3
GinTon wrote:
Sorry, I mean access to local variable from a method

import module
method(value)

I would to access to values that are created locally in that method
after the method has executed? usually the return value?
or you want to get all local variables, then make a func/method

def f(a=1):
b=2
c=3
return locals() #X/Object(locals())
--------
d=module.f()
print d['c'] # d.c

Robert
Nov 23 '06 #4
GinTon wrote:
Sorry, I mean access to local variable from a method

import module
method(value)
That's no access to a local variable of a method. It's a simple
function call.
I would to access to values that are created locally in that
method
Something with your interface seems horribly wrong.

Regards,
Björn

--
BOFH excuse #316:

Elves on strike. (Why do they call EMAG Elf Magic)

Nov 23 '06 #5
GinTon a écrit :
Sorry, I mean access to local variable from a method
One of the most surprising properties of local variables is that they
are, well... local.

Nov 23 '06 #6


GinTon,

I think this is what you want.
class Kdoi:
* *def __init__(self) : * *
* * * *self.Fdo()

* *def Fdo(self):

searchterm = 'help'
print searchterm #local

* * *self.searchterm = searchterm
* * *print self.searchterm #used inside the class

* * *Kdo.searchterm = searchterm #<<<<
* * *print Kdo.searchterm #used outside the class
Kdomore()
* *
class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()

def Fdomore(self):
searchterm = Kdo.searchterm # <<<<
print searchterm

jim-on-linux
http://www.inqvista.com


On Thursday 23 November 2006 17:09, GinTon wrote:
Sorry, I mean access to local variable from a
method

import module
method(value)

I would to access to values that are created
locally in that method

Fredrik Lundh ha escrito:
GinTon wrote:
How to access to a variable (that value is
not returned) from a module imported?
And the variable is set at the
module-level.
import module
print module.variable

(have you read the Python tutorial?)

</F>
Nov 23 '06 #7
Thanks Robert, the best solution is get all local variables, else is
impossible access to them.

robert ha escrito:
GinTon wrote:
I would to access to values that are created locally in that method

after the method has executed? usually the return value?
or you want to get all local variables, then make a func/method

def f(a=1):
b=2
c=3
return locals() #X/Object(locals())

--------

d=module.f()
print d['c'] # d.c
Nov 23 '06 #8
GinTon wrote:
Thanks Robert, the best solution is get all local variables, else is
impossible access to them.
if you don't want them to be local, why are you using local variables?

(have you read the Python tutorial?)

</F>

Nov 24 '06 #9

jim-on-linux wrote:
GinTon,

I think this is what you want.
class Kdoi:
Is that a typo?
def __init__(self) :
self.Fdo()
What is all this K and F stuff?
def Fdo(self):

searchterm = 'help'
print searchterm #local

self.searchterm = searchterm
print self.searchterm #used inside the class

Kdo.searchterm = searchterm #<<<<
print Kdo.searchterm #used outside the class
Kdomore()

class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()

def Fdomore(self):
searchterm = Kdo.searchterm # <<<<
print searchterm
It's not apparent what the print statements are for -- are they part of
an attempt to debug your code?

What gives you the idea that this is what the OP wants or needs?

Nov 24 '06 #10
GinTon wrote:
Thanks Robert, the best solution is get all local variables, else is
impossible access to them.
For test purposes/ex post inspection you could also uncomment the line in:

def f(a=1):
b=2
c=3
#globals().update(locals())
return a+b
--

then it is more easy and you can get it like:

module.c
You can also create a total stack trace dynamically with this trick function:

def mktb():
try: raise UserWarning
except: return sys.exc_info()[2]

def f(a=1):
b=2
c=3
global ftb;ftb=mktb()
return a+b
----

and then fully inspect the total situation in the func (and all down the call history) ex post at any time with
>>f()
pdb.post_mortem(module.ftb) # then do once 'up' in pdb/pywin.debugger...
pywin.debugger.post_mortem(module.ftb)

Which other programming language can do things like this?
( Unfortunately (legacy) Python has no possibility to (re-)continue execution from exceptions/traces other than by simple generators )

Robert
robert ha escrito:
>GinTon wrote:
>>I would to access to values that are created locally in that method
after the method has executed? usually the return value?
or you want to get all local variables, then make a func/method

def f(a=1):
b=2
c=3
return locals() #X/Object(locals())

--------

d=module.f()
print d['c'] # d.c
Nov 24 '06 #11


On Friday 24 November 2006 03:30, John Machin
wrote:
jim-on-linux wrote:
GinTon,

I think this is what you want.
class Kdoi:

Is that a typo?
No, it's a style. life seems to be easier
to me if one is consistent, all my classes begin
with K.
>
def __init__(self) :
self.Fdo()

What is all this K and F stuff?
It's my style. life seems to be easier to me
if one is consistent all my function begin with
F.

I started doing things like this when the only way
to debug was to read each line of code and try to
figgure out if it was the problem.
They are my personal sign posts.
def Fdo(self):
searchterm = 'help'
print searchterm #local

self.searchterm = searchterm
print self.searchterm #used inside the
class

Kdo.searchterm = searchterm #<<<<
print Kdo.searchterm #used outside the
class Kdomore()

class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()

def Fdomore(self):
searchterm = Kdo.searchterm #
<<<< print searchterm

It's not apparent what the print statements are
for -- are they part of an attempt to debug
your code?
print shows the results wherever a print statement
turns up the results = 'help' .
I didn't run the code, and it has it has a coding
error but if removed, the results should be;

searchterm = 'help'
self.searchterm = 'help'
Kdo.searchterm = 'help'

Sound silly but many people have trouble with
getting a variable from here to there in their
code. This shows that it can be done
What gives you the idea that this is what the
OP wants or needs?
If I remember right, he refrased his first
question and asked a second one.
Sometimes people don't take the time to write
correctly, the questions that are really in their
mind. So I guessed. If Im wrong, he will ignore
it. If I'm right, he will use it.

Also, I have found that other will latch on to the
ideas presented in these email responses. And
they will use them, even though the response was
not exactly what the original emailer wanted.

And, I sometimes I do use print statements to
debug, I have used other ways but on linux, I
prefer a print statement.

jim-on-linux
http://www.inqvista.com



Nov 24 '06 #12
On Friday 24 November 2006 13:01, jim-on-linux
wrote:
On Friday 24 November 2006 03:30, John Machin

wrote:
jim-on-linux wrote:
GinTon,
>
I think this is what you want.
>
>
class Kdoi:
Is that a typo?

No, it's a style. life seems to be
easier to me if one is consistent, all my
classes begin with K.
Sorry, Kdoi should be Kod
>
def __init__(self) :
self.Fdo()
What is all this K and F stuff?

It's my style. life seems to be easier to
me if one is consistent all my function begin
with F.

I started doing things like this when the only
way to debug was to read each line of code and
try to figgure out if it was the problem.
They are my personal sign posts.
def Fdo(self):
>
>
searchterm = 'help'
print searchterm #local
>
self.searchterm = searchterm
print self.searchterm #used inside the
class
>
Kdo.searchterm = searchterm #<<<<
print Kdo.searchterm #used outside the
class Kdomore()
the line above should be Kdomore(), not class
Kdomore() (For the technocrats)
>
>
>
class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()
>
def Fdomore(self):
searchterm = Kdo.searchterm #
<<<< print searchterm
It's not apparent what the print statements
are for -- are they part of an attempt to
debug your code?

print shows the results wherever a print
statement turns up the results = 'help' .
I didn't run the code, and it has it has a
coding error but if removed, the results should
be;

searchterm = 'help'
self.searchterm = 'help'
Kdo.searchterm = 'help'

Sound silly but many people have trouble
with getting a variable from here to there in
their code. This shows that it can be done
What gives you the idea that this is what the
OP wants or needs?

If I remember right, he refrased his first
question and asked a second one.
Sometimes people don't take the time to write
correctly, the questions that are really in
their mind. So I guessed. If Im wrong, he will
ignore it. If I'm right, he will use it.

Also, I have found that other will latch on to
the ideas presented in these email responses.
And they will use them, even though the
response was not exactly what the original
emailer wanted.

And, I sometimes I do use print statements to
debug, I have used other ways but on linux, I
prefer a print statement.
jim-on-linux
http://www.inqvista.com
Nov 24 '06 #13
On Friday 24 November 2006 13:20, jim-on-linux
wrote:
On Friday 24 November 2006 13:01, jim-on-linux

wrote:
On Friday 24 November 2006 03:30, John Machin

wrote:
jim-on-linux wrote:
GinTon,

I think this is what you want.


class Kdoi:
>
Is that a typo?
No, it's a style. life seems to be
easier to me if one is consistent, all my
classes begin with K.

Sorry, Kdoi should be Kod
Sorry again Kdoi should be Kdo
(Haste makes waste.)
>
def __init__(self) :
self.Fdo()
>
What is all this K and F stuff?
It's my style. life seems to be easier to
me if one is consistent all my function begin
with F.

I started doing things like this when the
only way to debug was to read each line of
code and try to figgure out if it was the
problem. They are my personal sign posts.
def Fdo(self):


searchterm = 'help'
print searchterm #local

self.searchterm = searchterm
print self.searchterm #used inside
the class

Kdo.searchterm = searchterm #<<<<
print Kdo.searchterm #used outside
the class Kdomore()

the line above should be Kdomore(), not class
Kdomore() (For the technocrats)
class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()

def Fdomore(self):
searchterm = Kdo.searchterm #
<<<< print searchterm
>
It's not apparent what the print statements
are for -- are they part of an attempt to
debug your code?
print shows the results wherever a print
statement turns up the results = 'help' .
I didn't run the code, and it has it has a
coding error but if removed, the results
should be;

searchterm = 'help'
self.searchterm = 'help'
Kdo.searchterm = 'help'

Sound silly but many people have trouble
with getting a variable from here to there in
their code. This shows that it can be done
What gives you the idea that this is what
the OP wants or needs?
If I remember right, he refrased his first
question and asked a second one.
Sometimes people don't take the time to write
correctly, the questions that are really in
their mind. So I guessed. If Im wrong, he
will ignore it. If I'm right, he will use
it.

Also, I have found that other will latch on
to the ideas presented in these email
responses. And they will use them, even
though the response was not exactly what the
original emailer wanted.

And, I sometimes I do use print statements to
debug, I have used other ways but on linux, I
prefer a print statement.
jim-on-linux
http://www.inqvista.com
Nov 24 '06 #14

jim-on-linux wrote:
On Friday 24 November 2006 03:30, John Machin
wrote:
jim-on-linux wrote:
GinTon,
>
I think this is what you want.
>
>
class Kdoi:
Is that a typo?
No, it's a style. life seems to be easier
to me if one is consistent, all my classes begin
with K.
and end with "i"?
def __init__(self) :
self.Fdo()
What is all this K and F stuff?
It's my style. life seems to be easier to me
if one is consistent all my function begin with
F.
You left out a word; the correct way of phrasing that is: "All my
function _are_ begin with F" :-)

This appears to be a variation on "Hungarian notation"; google that for
opinions pro & con.

In a certain vernacular, it would be called "an effed concept" :-)
>
I started doing things like this when the only way
to debug was to read each line of code and try to
figgure out if it was the problem.
When was that? Even years ago, there were slightly better ways. For
example, my first boss' boss was an enthusiastic coder and debugger and
also a workaholic. Colleagues who lived along the same railway line as
he and were foolish enough not to hide behind a newspaper could have
their morning or evening reverie disturbed by a cry of "Glad you're
here! I'll hold the listing, you hold the dump!". I get the impression
that debugging techniques have moved along a little bit since then. :-)
They are my personal sign posts.
def Fdo(self):
>
searchterm = 'help'
print searchterm #local
>
self.searchterm = searchterm
print self.searchterm #used inside the
class
>
Kdo.searchterm = searchterm #<<<<
print Kdo.searchterm #used outside the
class Kdomore()
>
>
>
class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()
>
def Fdomore(self):
searchterm = Kdo.searchterm #
<<<< print searchterm
It's not apparent what the print statements are
for -- are they part of an attempt to debug
your code?
print shows the results wherever a print statement
turns up the results = 'help' .
I didn't run the code, and it has it has a coding
error
I noticed.
but if removed, the results should be;

searchterm = 'help'
self.searchterm = 'help'
Kdo.searchterm = 'help'
No, the result would be
help
help
help

Plug in a text-to-speech module and a phone dialer and you're done ;-)
>
Sound silly but many people have trouble with
getting a variable from here to there in their
code. This shows that it can be done
What gives you the idea that this is what the
OP wants or needs?

If I remember right, he refrased his first
question and asked a second one.
Sometimes people don't take the time to write
correctly, the questions that are really in their
mind. So I guessed. If Im wrong, he will ignore
it. If I'm right, he will use it.
With luck. Kindly consider another possibility: that you are wrong (or
just marching to the beat of your own tambourine) and he (or she) is a
newbie & will use it :-)

[snip]

HTH,
John

Nov 24 '06 #15
On Friday 24 November 2006 13:41, John Machin
wrote:
jim-on-linux wrote:
On Friday 24 November 2006 03:30, John Machin

wrote:
jim-on-linux wrote:
GinTon,

I think this is what you want.


class Kdoi:
>
Is that a typo?
No, it's a style. life seems to be
easier to me if one is consistent, all my
classes begin with K.

and end with "i"?
def __init__(self) :
self.Fdo()
>
What is all this K and F stuff?
It's my style. life seems to be easier to
me if one is consistent all my function begin
with F.

You left out a word; the correct way of
phrasing that is: "All my function _are_ begin
with F" :-)
No, for Non-Hungrian programmers it's "all-ah me"
Functions gona begin witha F, not Func. anda
"all-ah-me" classes gona begin witha K, not Klas.
Anda only me gona Know the Fdiff cause me codea is
not opena. Anda I finda that it savea me time
causea I doa thisa way fora a longa time.

Whena I gonna hava to changea maybe I willa.
>
This appears to be a variation on "Hungarian
notation"; google that for opinions pro & con.

In a certain vernacular, it would be called "an
effed concept" :-)
I started doing things like this when the
only way to debug was to read each line of
code and try to figgure out if it was the
problem.

When was that?
That was when bill gates just left Harvard,
basic was brand new, and 4k of memory was
installed free when you bought a computer,
(TRS80), my first,. Assemble was the alternative
to Basic and you had to backup on tape because
floppies didn't exist. And, most people on this
site wern't even a gleem in their fathers eye.
Even years ago, there were
slightly better ways. For example, my first
boss' boss was an enthusiastic coder and
debugger and also a workaholic. Colleagues who
lived along the same railway line as he and
were foolish enough not to hide behind a
newspaper could have their morning or evening
reverie disturbed by a cry of "Glad you're
here! I'll hold the listing, you hold the
dump!". I get the impression that debugging
techniques have moved along a little bit since
then. :-)
They are my personal sign posts.
def Fdo(self):


searchterm = 'help'
print searchterm #local

self.searchterm = searchterm
print self.searchterm #used inside
the class

Kdo.searchterm = searchterm #<<<<
print Kdo.searchterm #used outside
the class Kdomore()



class Kdomore(Kdo):
def __init__(self) :
self.Fdomore()

def Fdomore(self):
searchterm = Kdo.searchterm #
<<<< print searchterm
>
It's not apparent what the print statements
are for -- are they part of an attempt to
debug your code?
print shows the results wherever a print
statement turns up the results = 'help' .
I didn't run the code, and it has it has a
coding error

I noticed.
but if removed, the results should be;

searchterm = 'help'
self.searchterm = 'help'
Kdo.searchterm = 'help'
Correct but when writing one must be clear.

Would it be better for me to write, your question
above was Is that a typo?

Or is it better if I were to write, your
question above, "Is that a typo?",
is a legimate question, but not clear.

So, to be clear one might write is "Kdoi"
correct?.

A clear response would be, it is not "Kdoi", it is
"Kdo".

But that's not correct either, it is Kdo.

If one runs the code I don't expect the user to
look for "help", I think we will see help and
will THINK that the results are correct.

THINK is also incorrect, it should be written.
think, or should it?

No, the result would be
help
help
help

Plug in a text-to-speech module and a phone
dialer and you're done ;-)
Sound silly but many people have trouble
with getting a variable from here to there in
their code. This shows that it can be done
What gives you the idea that this is what
the OP wants or needs?
If I remember right, he refrased his first
question and asked a second one.
Sometimes people don't take the time to write
correctly, the questions that are really in
their mind. So I guessed. If Im wrong, he
will ignore it. If I'm right, he will use
it.

With luck. Kindly consider another possibility:
that you are wrong (or just marching to the
beat of your own tambourine) and he (or she) is
a newbie & will use it :-)
"Because he being of course judge of that tendency
will make his opinions the rule of judgment, and
approve or condemn the sentiments of others only
as they shall square with or differ from his own…"
Thomas Jefferson

I enjoied this, but time is money,

jim-on-linux
http://www.inqvista.com


Nov 24 '06 #16
On Fri, 24 Nov 2006 16:56:58 -0500, jim-on-linux wrote:
Correct but when writing one must be clear.
[jaw drops]

Given the number of typos your posts include, the mock accent, the
nonsensical sentences, the annoying hard-to-read coding conventions, and
the sheer number of grammatical errors in your sentences, do you have any
idea of the irony of that statement?

No, I imagine you don't.

Oh, and don't flatter yourself that you're the Old Man of Programming,
compared to all the young whipper snappers on this list who, quote,
"wern't even a gleem in their fathers eye". Many of us have been around
quite a while, some of us even remember that there was a computer market
before Bill Gates went dumpster-diving for source-code for a BASIC
interpreter, and even if we weren't, there is nothing to be proud of using
1970s programming style in 2000s programming languages.
--
Steven.

Nov 24 '06 #17

The TRS-80 I bought came with both Basic and
Assembly Language teaching guides, and that was
it. To make the machine work one had to program.
I didn't mean to imply that Bill Gates developed
it. It's well known that MS borrowed stuff when
they needed to from where ever the could get it.
That's business.

I'm not an MS fan but Bill Gates was the one who
gave away a very cheep, borrowed but improved,
copy of DOS to computer sellers. These copies
could also be copied to floppies (8 inch). So,
DOS 3.3 was used by computer sellers, to install
DOS on the buyers machine, (intel 286) free.

On the other hand, IBM sold the same package for
$50.00.

I got the free copy of MS 3.3 with my 286.

After that, Windows 3.0 cost me $25.00,
Windows 3.1 cost me $30.00,
DOS upgrade from3.3 to 6.22 cost
me $55.00.
Since then I purchased Win 95, $100.00
and Win 98. $125.00.
And, all for testing software that I produced for
people that use that stuff.

Bill Gates probably can't program any software to
write "Hello World" on any screen, but I'll bet
he knows how to fill out a deposit ticket.

I think Bill Gates recognize early that the money
is in the marketing of the product, not the
programming of it. How else can you explain the
success of Windows, like it or not?

jim-on-linux
http://www.inqvista.com


On Friday 24 November 2006 17:18, Dennis Lee
Bieber wrote:
On Fri, 24 Nov 2006 16:56:58 -0500,
jim-on-linux <in*****@verizon.net>

declaimed the following in comp.lang.python:
That was when bill gates just left Harvard,
basic was brand new, and 4k of memory was

Pardon? I'd learned BASIC back around 1972, in
the 9th grade, using an ASR-33 with dial-up to
some company's Honeywell-Bull system.

BASIC is one of the ancients in languages,
predating Pascal and C.

Just because Gates managed to scrabble
together a BASIC interpreter for the MITS
Altair, and then had it picked up by other
makers of 8080/Z-80 based "microcomputers"
doesn't make it "brand new". (Personally, I
suspect he hasn't done any programming ever
since that day, and is probably still trying to
find some way to sue Kemeny&Kurtz (sp?s) over
their own creation)
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support
Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
Nov 24 '06 #18

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

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: Darren | last post by:
I have been attempting to create a reservation planning form in excel that imports Data from an Access database and inserts that information automaticly into the correct spreed sheet and the...
3
by: ghadley_00 | last post by:
Hi, Does anyone know of a way I can write a script that imports a table from a particular .mde file? I don't seen any obvious way to do it as a macro. Any suggestions if it can be done as a...
3
by: Marco Aschwanden | last post by:
Hi I have a script that looks for modules and tries to load them. After loading I attach the "server"-API function to it. module = __import__(module_name, globals(), locals(), ) module.server...
1
by: puremetal33 | last post by:
I have worked very little with Access and have hit a snag. My task right now is to import the data from a spreadsheet into an existing table in an Access database. I edited the .xls file so that...
4
by: noagbodjivictor | last post by:
I have a variable names actions in a module named qt_actions.py Well this is what I get: Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object...
1
by: hjeff71 | last post by:
Ok, we've got this complicated form with 10000s of lines of vba code, doing all sorts of calculation, The forms are on several pcs, and the tables are on the server, (using the external link) ...
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.