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

8
  • First import in second example seems wrong; please check, whether I adjusted indentation matching to what you have. Commented Jul 21, 2020 at 7: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 Commented Jul 21, 2020 at 7:23
  • You should note, your property is completely pointless. You should just use a regular attribute, because your getter and setter do nothing. Commented Jul 21, 2020 at 7:37
  • 1
    Anyway, "The property cannot be accessed. " what did you try exactly and how did it not work, exactly? Always provide a minimal reproducible example and any error messages or a complete description of the incorrect behavior (which would include a complete description of the expected behavior. Commented Jul 21, 2020 at 7: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. Commented Jul 21, 2020 at 7:52

2 Answers 2

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']) 
Sign up to request clarification or add additional context in comments.

Comments

-1

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

Importing class from another file

Importing files from different folder

2 Comments

thanks for participating. we both learn together :) Both of the files is already in the same folder
Sorry for that misunderstanding

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.