0

How to request a variable value from a module in python

I know this question may be very stupid and may be in the course of python but you could tell me if you can request a specific variable belonging to a module, if so Let me explain, I want to transform this code (https://code.sololearn.com/cQA1ezE9CvGT/?ref=app) into a modular (https://code.sololearn.com/c9paVJ0x7oe1/?ref=app) code to make it more comfortable for me but I'm not very good at making modules and I keep learning In the code I'm doing I want to get the variable entrenamiento module model, but I don't know how Upgrade Hi I've managed to do it correctly I just need to solve the last part of the code where I need the variable "historial" to do the plt.plot(historial.history["loss"]) but "historial" belongs to "entrenamiento()" how do I use that variable?

18th Sep 2021, 8:25 PM
Jcrex
Jcrex - avatar
8 Réponses
+ 1
Import m print(m.v)
18th Sep 2021, 8:44 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 1
Do you want to create a module? If so you need to create a file like this: h.py put it in the Lib folder, Open it in an editor, write whatever you want inside, and import it.
18th Sep 2021, 9:02 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 1
It's a local variable and to use it out of the function you have to make it global: def f(): global a a = 1 And before using it, you must call the function at least once.
18th Sep 2021, 10:36 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 1
It does not matter if your code is in a module or in the main body. In both cases the variable inside a function cannot be used outside it. You either have to define the variable outside the function, or you have to make it global: def func(): global a a = "hi" Now "a" is accessible outside the function. But the function must be called at least once before using the "a", otherwise the code inside it is not executed and for this reason, "a" is not defined at all. function calling can occure in the module or in the main body. In the module: Fun() In the main body: import h h.fun() And then you can use "a" in the main body. print(h.a) Let me know if i did not explain somthing well enough.
19th Sep 2021, 4:58 AM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
0
I just updated the question I hope it is better understood
18th Sep 2021, 8:53 PM
Jcrex
Jcrex - avatar
0
Hi I've managed to do it correctly I just need to solve the last part of the code where I need the variable "historial" to do the plt.plot(historial.history["loss"]) but "historial" belongs to "entrenamiento()" how do I use that variable?
18th Sep 2021, 9:57 PM
Jcrex
Jcrex - avatar
0
You could explain it with an example I tried to do it but I don't understand
18th Sep 2021, 11:24 PM
Jcrex
Jcrex - avatar
- 1
Your question is somewhat unclear. Can you give us more information about what you're trying to do?
18th Sep 2021, 8:40 PM
Simon Sauter
Simon Sauter - avatar