sharing vars with different functions

S

scripteaze

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
 
M

Marc 'BlackJack' Rintsch

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

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
 
M

Martin Marcher

I hate gmail, always forgetting to set the right recipient...


---------- Forwarded message ----------
From: Martin Marcher <[email protected]>
Date: 29.10.2007 10:11
Subject: Re: sharing vars with different functions
To: "(e-mail address removed)" <[email protected]>


2007/10/29 said:
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?
.... 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'
 
B

Bruno Desthuilliers

(e-mail address removed) 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

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top