Reputation: 8376
I want to get a variable which can be used at any method from any class.
I started like this:
class clasificadorDeTweet:
"""Funciones para clasificar tweets"""
def fx():
print trendingTopics
def iniciarConjuntoTrendingTopics():
trendingTopics = ['EPN', 'PRI', 'Peña Nieto', 'México', 'PresidenciaMX', 'Osorio Chong', 'SEDENA', 'PEMEX', 'Reforma Energética', 'Guerra contra el narco', 'Reforma Fiscal', 'Los Pinos'];
return set(trendingTopics)
global trendingTopics
trendingTopics = iniciarConjuntoTrendingTopics()
x=clasificadorDeTweet()
x.fx
But I'm not getting anything printed. What have I got wrong?
Upvotes: 0
Views: 56
Reputation: 319
If you want acces to trendingTopics, you have to declare trendingTopics as global on the method scope
def fx(): global tredingTopics print tredingTopics
Upvotes: 0
Reputation: 89847
You need to call the method x.fx
by adding ()
after it:
x.fx()
However, this isn't your only problem; fx
is an instance method, so it needs to take self
(by convention; you can call it anything you want, but you need to have some parameter) as the first parameter.
You probably also want to use unicode strings in trendingTopics
and declare a source encoding at the start of your file; embedding non-ASCII str
s in your code may coincidentally work perfectly fine, then break horribly when moved to another system.
Your global
statement at the module level also does literally nothing at all. Variables assigned at the module scope are already globals.
Upvotes: 1