Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2887 connectés 

  FORUM HardWare.fr
  Programmation
  Python

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

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

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

n°844909
lint
O_O
Posté le 08-09-2004 à 11:54:34  profilanswer
 

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 ...


---------------
\@/
mood
Publicité
Posté le 08-09-2004 à 11:54:34  profilanswer
 

n°844923
lorill
Posté le 08-09-2004 à 12:21:26  profilanswer
 

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

n°844927
masklinn
í dag viðrar vel til loftárása
Posté le 08-09-2004 à 12:24:17  profilanswer
 

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 by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
n°844933
lint
O_O
Posté le 08-09-2004 à 12:31:25  profilanswer
 

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

---------------
\@/
n°844963
masklinn
í dag viðrar vel til loftárása
Posté le 08-09-2004 à 13:05:58  profilanswer
 

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 by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
n°844997
lint
O_O
Posté le 08-09-2004 à 13:49:04  profilanswer
 

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 :/


---------------
\@/
n°845010
masklinn
í dag viðrar vel til loftárása
Posté le 08-09-2004 à 14:00:33  profilanswer
 

-_-
 
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 by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
n°845022
lint
O_O
Posté le 08-09-2004 à 14:25:29  profilanswer
 

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


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

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Python

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

 

Sujets relatifs
Pb de portée de variable sur fichiers include[python] import de fichiers, comment ca marche ?
Recuperer le contenu d'un fichier en variablePython pour remplacer une legacy app?
Concater un objet pour former une variablePasser le resultat d'une commande shell en variable ?
zone de notification en python[JAVASCRIPT][Newbie] Petit soucis de récupération de variable
variable globale, problème de link[ACCESS] Insert° d'1 moyen. pond. ds la table servant à la calculer
Plus de sujets relatifs à : [python] y'a moyen d'externaliser une variable ?


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)