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

sharing vars with different functions

Im tryin to call a var thats sitting in a function, example:

class someclass(object):
somevar = open(blah, 'r').readlines()
def something():

for line in somevar:
print line
-----------------------------------------------------------------------

i guess im not graspng the whole global or local var topic..

Any examples??

Oct 29 '07 #1
3 1106
On Mon, 29 Oct 2007 09:03:14 +0000, sc********@gmail.com wrote:
Im tryin to call a var thats sitting in a function, example:

class someclass(object):
somevar = open(blah, 'r').readlines()
Thats a class variable. Is that really what you want!?
def something():

for line in somevar:
print line
-----------------------------------------------------------------------

i guess im not graspng the whole global or local var topic..
There is no real global here. One is (somewhat) local to the class the
other local to the function.
Any examples??
def something(lines):
for line in lines:
print lines

And the call it with the object.

Ciao,
Marc 'BlackJack' Rintsch
Oct 29 '07 #2
I hate gmail, always forgetting to set the right recipient...
---------- Forwarded message ----------
From: Martin Marcher <ma****@marcher.name>
Date: 29.10.2007 10:11
Subject: Re: sharing vars with different functions
To: "sc********@gmail.com" <sc********@gmail.com>
2007/10/29, sc********@gmail.com <sc********@gmail.com>:
Im tryin to call a var thats sitting in a function, example:

i guess im not graspng the whole global or local var topic..
does this help?
>>class Foo(object):
.... somevar = ["a", "b"]
.... def __init__(self):
.... self.another_var = ["c", "d"]
....
>>Foo.somevar
['a', 'b']
>>f = Foo()
f.somevar
['a', 'b']
>>f.another_var
['c', 'd']
>>Foo.somevar = "New Value"
f.somevar
'New Value'
>>Foo.another_var
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Foo' has no attribute 'another_var'

--
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours
--
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours
Oct 29 '07 #3
sc********@gmail.com a écrit :
Im tryin to call
s/call/access/
a var thats sitting
s/sitting/defined/
in a function, example:
In this example, s/function/class/
class someclass(object):
pep08 : should be SomeClass(object):
somevar = open(blah, 'r').readlines()
Doing IO in the body of a class statement is IMHO a *very* bad idea.
>
def something():

for line in somevar:
print line
-----------------------------------------------------------------------

i guess im not graspng the whole global or local var topic..
Seems there are a couple other points you're net yet grasping. Anyway,
in this case, you should pass the iterable object to your function:

def something(iterable):
for line in iterable:
print line

something(someclass.somevar)
Oct 29 '07 #4

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

Similar topics

1
by: CanardWC | last post by:
Hello, Just a little problem. I want to share a shopping cart between 2 php sites for example foo and fii. There'is no login / password solution wanted, so i must use cookie and session. If...
0
by: james | last post by:
I am new to php and need some help getting the session variables into include files. (after-thought, Sorry for the drawn out post but I really, really need help....;) Here's what I'm doing.. ...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
2
by: Guy | last post by:
Is there a way to prevent new browsers windows from sharing session variables with the original window? Our team has an ASP.Net app that lets users analyze portfolio risk given certain portfolio...
0
by: rcolby | last post by:
Hi, A little advice on code sharing across projects, namespaces and dll's for references. I've got an application that is split between three different projects (one for a service, one for...
4
by: pcaisse | last post by:
I'm having issues sharing global variables with Explorer. This problem probably has a simple answer (as with most newbie questions). The script.pl file: #!/usr/bin/perl -w use strict; use...
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
6
by: goodguyjam | last post by:
Hi all, I'm having trouble with mysql. I've just finished my php coding for HTTP authentication and with some help am now getting a login window pop up whenever I click on a link on my website...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.