2

I have a code which has few Classes in it. Under SecondClass, there is a function called plot_image. Under this function, there are three variables defined called my_x, my_y, my_z. I am trying to make these variables accessible inside some other function called get_histogram in the same Class. Here is a non-working example in the form of a pseudo-snippet:

import some_modules_in_here #these are required for classes below

#===================================================================================================
# utility
#===================================================================================================
class FirstClass(something.something.SayClass):
    def file1(self, some_arguments):
        pros = do_something
        return pros


#===================================================================================================
# visualize
#===================================================================================================
class SecondClass(something.something.SayClass):

    def plot_image(self, some_parameter):
        if some_parameter is not None:
            my_x = some_value1
            my_y = some_value2
            my_z = some_value3

    def get_histogram(self, some_parameter, some_other_parameter, return_info):
        if '3d' in some_parameter:
            do_this
        else:
            do_that 
            if some_other_parameter:
                calculate_something
                if return_info:
                    for k, l, m in zip(my_x, my_y, my_z):
                        do_some_stuff
                        print(something_in_here)
        return_value = do_something_else_in_here
        return  return_value

.
.
.

    def print_values(self):
        '''
        write some values of image to file.
        '''

Second = SecondClass()

I tried the keyword global at the top of the function plot_image but that doesn't do the job for me giving error message:

"NameError: Name 'my_x' is not defined."

What is the solution?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Rebel
  • 472
  • 8
  • 25
  • 1
    Then store them in the instance's `self` space, i.e. `self.my_x = some_value` etc. Then when you need them, read them from the `self` as well. – zwer Dec 08 '17 at 03:17
  • @zwer, thanks for the response, would you elaborate more on where should I insert the line `self.my_x = some_value` in the code above? and by reading it from 'self,' do you mean inside `zip`, I should insert `self.my_x` instead of `my_x`? – Rebel Dec 08 '17 at 04:33
  • Added that to the end of `plot_image` function just before `get_histogram` (where they are needed) and then called them as you said but receiving: for k, l, m in zip(my_x, my_y, my_z): AttributeError: 'SecondClass' object has no attribute 'my_x' – Rebel Dec 08 '17 at 05:07
  • 1
    for k, l, m in zip(**self**.my_x, **self**.my_y, **self**.my_z) ... You really should read up on OOP in Python, how it works and what `self` means. Any detailed tutorial will do, for example [this one](https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/) – zwer Dec 08 '17 at 09:19
  • It's very complicated abstract ideas and I have only learned to do simple things:( Part of the issue is that the code that I am working on is very long(~5000) lines and I am modifying it to fit into my purpose. Maybe if I had a smaller code, would have been easy to manage. This is why I couldn't create a minimal working example. I tried to define `__init__` for my `SecondClass` (which was missing) and in it, I put those three variables as you stated. But, right at the runtime, it says that those variables are not defined. Should I stick that `if statement` with desired variables in a function? – Rebel Dec 08 '17 at 19:54
  • @zwer, it seems I established that connection between the desired variables and the instances defined in the top of the class. However, I am receiving an error message of a different nature saying, *TypeError: zip argument #1 must support iteration*. Could you speculate as to why that's the case? out of *my_x*, *my_y*, and *my_z,* two are lists and one is tuple. – Rebel Dec 12 '17 at 23:45
  • I also think that without a need of defining `__init__()` under the Class, I can actually make the needed variables (*my_x*, *my_y*, *my_z*) under `If statement` accessible *anywhere* in the code where they are needed. I guess this is what *global variables* do, but I don't know how to implement this idea. – Rebel Mar 15 '18 at 22:39
  • Does this answer your question? [Passing variables between methods in Python?](https://stackoverflow.com/questions/9520075/passing-variables-between-methods-in-python) – Karl Knechtel Sep 10 '22 at 10:03

0 Answers0