1

I am new in Python programming. I have property consist of getter and setter in a class in file A.

class Histogram:
    def getHue(self):
        return self.value

    def setHue(self, value):
        self.varHueHist = value

    HueHist = property(getHue, setHue)

I want to access the property from another class in another file which is file B. But after I import the file A and the class inside the file B, the property still can not be accessed.

from Histogram import Histogram

class moment:
   var = Histogram()

The property cannot be accessed. When i tried to use automatic completing (Ctrl+space) in my Spyder, the property also didn't show. Unfortunately, when i tried to access the property inside class in the same file with the property, the property is readable. How to call property from inside class in another file?

Both of the files are already in the same folder. Please help. Thanks

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Ines
  • 11
  • 5
  • First import in second example seems wrong; please check, whether I adjusted indentation matching to what you have. – guidot Jul 21 '20 at 07:15
  • @guidot sorry, that was a mistake from me when i write this question. But in code the one i used is the edited answer just now. I use `from filename import class_name` for my code – Ines Jul 21 '20 at 07:23
  • You should note, your `property` is completely pointless. You should just use a regular attribute, because your getter and setter do nothing. – juanpa.arrivillaga Jul 21 '20 at 07:37
  • 1
    Anyway, "The property cannot be accessed. " what did you try *exactly* and how did it not work, *exactly*? Always provide a [mcve] and any error messages or a complete description of the incorrect behavior (which would include a complete description of the expected behavior. – juanpa.arrivillaga Jul 21 '20 at 07:38
  • @juanpa.arrivillaga i want to make a new histogram in another class in another file and use histogram that using only color hue. So i make the `property Hue` that wil get the number of Bin used for making histogram. So when i call it from another class it will be like this, first I make instance class and store it in a variable: `CandidateHistogram = Histogram()` and then i call the property to make histogram like this: `CandidateHistogram.HueHist(20)`. I want to create histogram by calling the statement like that. – Ines Jul 21 '20 at 07:52
  • @juanpa.arrivillaga I use Spyder to code, and I use Kite auto completion, when I try to call the `property` from another file like i tell previously, the `property` didn't show in the pop up auto completion (Ctrl+space). So I guess the technique or my code is not functioning – Ines Jul 21 '20 at 07:56
  • @Ines Please [edit](https://stackoverflow.com/posts/63009199/edit) your question to include this information there. Again, **please** provide a [mcve], something that others can copy-and-paste and reproduce the errors you are seeing. It is still not clear to me what your problem is. Your described use-case seems to have nothing to do with `property`, which is a built-in descriptor to define accessors and mutators which will use `some_object.some_attribute` and `some_object.some_attribute = value` syntax. It sounds like you want to define a regular method. – juanpa.arrivillaga Jul 21 '20 at 08:31
  • Nowhere a caller of the property, of the routines it consists of, or a direct setting of the `value` attribute appears (a constructor would be a fine place for initializing). Are you sure, it is set in your second file creating the `var` instance? – guidot Jul 21 '20 at 14:52

2 Answers2

0

What you trying to do is achieved using descriptors. You can see more here:

https://docs.python.org/3/howto/descriptor.html

So your code should looks like this:

class Histogram:
 
  def __set_name__(self, owner, name):
     self.public_name = name
     self.private_name = '_' + name

  def __get__(self, obj, objtype=None):
     return getattr(obj, self.private_name)

  def __set__(self, obj, value):
     setattr(obj, self.private_name, value)

Now you can call it, and it will work as a descriptor setting and getting accordingly.

import Histogram

class Moment:
    var = Histogram()

Also files being in same folders does not means the imports will work. You should create a init.py empty file inside the folder and then do

from folder.file import class_name

Or you can add the folder to the path:

import sys
sys.path.extend(['complete_path_to_the_folder_where_code_is'])
Leonardo Hermoso
  • 838
  • 12
  • 26
-1

I'm not a professional but, i think your question is answered here

Importing class from another file

Importing files from different folder

Dharman
  • 30,962
  • 25
  • 85
  • 135
omar_9
  • 19
  • 1
  • 6