[python] y'a moyen d'externaliser une variable ?

y'a moyen d'externaliser une variable ? [python] - Python - Programmation

Marsh Posté le 08-09-2004 à 11:54:34    

dans mon cas c'est un dictionary déclaré en global dans un fichier que j'aimerais pouvoir modifier dans un autre
 
pour l'instant j'ai ca :
 
fichier1.py

Code :
  1. import fichier2
  2. d = {}
  3. def func1():
  4.     d["key"] = "value"
  5.     d["monty"] = "python"
  6.     print "fichier1:%s" % (d,)
  7.     fichier2.func2()
  8.     print "fichier1:%s" % (d,)
  9. if __name__ == '__main__':
  10.     func1()


 
fichier2.py

Code :
  1. import fichier1
  2. def func2():
  3.     fichier1.d["monty"] = "foo"
  4.     fichier1.d["value"] = "bar"
  5.     print "fichier2:%s" % (fichier1.d,)


 
output

>python -u "fichier1.py"
fichier1:{'monty': 'python', 'key': 'value'}
fichier2:{'value': 'bar', 'monty': 'foo'}
fichier1:{'monty': 'python', 'key': 'value'}
>Exit code: 0


 
 
j'aimerais éviter d'avoir a retourner les valeurs :/
 
idée ? je prends ...


---------------
\@/
Reply

Marsh Posté le 08-09-2004 à 11:54:34   

Reply

Marsh Posté le 08-09-2004 à 12:21:26    

ben ca marche la... C'est quoi le probleme :??:
 
(a part ca : les variables globales, c'est pas vraiment une bonne idée)

Reply

Marsh Posté le 08-09-2004 à 12:24:17    

il veut que son accès à fichier1.d modifie d dans fichier 1
 
lint -> les deux "d" n'appartiènnent pas à la même instance, le d utilisé dans fichier 2 appartient à un import spécial de fichier 1 et complètement indépendant du code se déroulant dans fichier 1


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 08-09-2004 à 12:31:25    

bah justement j'aimerais que les modifs apportées a d dans fichier2 soient visibles dans fichier1 :/
 
edit: polio [:leg9]


Message édité par lint le 08-09-2004 à 12:34:53

---------------
\@/
Reply

Marsh Posté le 08-09-2004 à 13:05:58    

dans fichier 2, essaie en enlevant l'import fichier1, toutes les références à fichier 1 (remplaces fichier1.d par d) et juste après le def func2 écrit "global d"


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 08-09-2004 à 13:49:04    

fichier2.py

Code :
  1. def func2():
  2.     d["monty"] = "foo"
  3.     d["value"] = "bar"
  4.     print "fichier2:%s" % (d,)
  5. global d


 
output

>python -u "fichier1.py"
fichier1:{'monty': 'python', 'key': 'value'}
Traceback (most recent call last):
  File "fichier1.py", line 13, in ?
    func1()
  File "fichier1.py", line 9, in func1
    fichier2.func2()
  File "C:\dev\Repository\xbc\src\test\fichier2.py", line 2, in func2
    d["monty"] = "foo"
NameError: global name 'd' is not defined
>Exit code: 1


 
pareil si "global d" est au dessus de func2 :/


---------------
\@/
Reply

Marsh Posté le 08-09-2004 à 14:00:33    

-_-
 
quand je dis "juste après def func2"
ca veut dire

Code :
  1. def func2():
  2.     global d
  3.     ...


si ca marche pas (ya de fortes chances que ca marche pas), tu essaies de passer d en argument de func2


Message édité par masklinn le 08-09-2004 à 14:01:35

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 08-09-2004 à 14:25:29    

désolé :/
 
la dernière solution marche, je pense que ca fera l'affaire
merci :)


---------------
\@/
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed