1
tree get_info 
get_info
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-38.pyc
│   └── get_resources.cpython-38.pyc
└── get_resources.py

contents of get_resources.py

class get_resource_info():
    def __init__(self):
        self.color = 'red'

    def print(self):
        print("hi printing")
        print(self.color)

I have the following file test.pyhere:

#!/usr/bin/env python

from get_info import get_resource_info

class test():
    def __init__(self):
        pass
    def test(self):
        get_resource_info.print(self)
if __name__ == '__main__':
    tes = test()
    tes.test()

from above structure, I do not understand the following: when I run test.py - I get the following output:

hi printing
Traceback (most recent call last):
  File "./test.py", line 17, in <module>
    tes.test()
  File "./test.py", line 9, in test
    get_resource_info.print(self)
  File "/get_info/get_resources.py", line 14, in print
    print(self.color)
AttributeError: 'test' object has no attribute 'color'

couple of things that are not making sense to me:

  1. when I call the get_resource_info.print module from get_resource_info object why do i have to pass an argument (self)
  2. i dont understand why it is complaining that there is no attribute color -- my understanding is, when I call the object get_resource_info, it will first initiate the __init__ module and set its attributes!! , in this case it should have set the attribute color

please let me know what I am understanding wrong here. will appreciate any help I can get.

i have looked at this: Importing class from another file to make __init__.py work

I have also looked at: Python: How to access property from another file ? from file name import class didn't work

and other resources but I am not able to figure it out unfortunately, so please let me know what is going on here!!

Prabin

Prab
  • 464
  • 3
  • 13
  • What is in "file.py"? – Michael Butscher Oct 09 '22 at 22:27
  • 2
    1) your using `get_resource_info.print(self)` on the class get_resource_info and not an instance. Instances, like `tes.test()` will pass themself as the first argument. On a class there is no automatic insertion of a first argument. `get_resource_info().print() # this will work` 2) Now you pass `self` to `get_resource_info.print`, `self` at that position in the tes object, which has no color attribute, likewise the `get_resource_info` class has not - only instances of it. `print(self, type(self))` might help you to further track down what's going on. – Daraan Oct 09 '22 at 22:31
  • @MichaelButscher apologies, replaced it with test.py. – Prab Oct 09 '22 at 22:33
  • @Daraan gotcha, thank you using ***get_resource_info().print()*** worked. but here is my follow up question. when we import a library such as `os`, we call its methods by `os.walk`, we dont do `os().walk()`. So why is this the case for those library! it may be a stupid question, and sorry for that. – Prab Oct 09 '22 at 22:46
  • Modules and classes are quite similar from a high level perspective and their usage. Except that classes are able to create instances by calling them (modules can't be called). Instances then can hold individual attributes. Classes and modules can hold exactly one value for their attributes. In your case it is similar you have `get_info{module}.get_resource_info(){class}` likewise `os{module}.walk(){function}` or `os{module}.PathLike(){class}` python allows us to drop the module prefix via imports. Hope this helps – Daraan Oct 10 '22 at 07:49
  • Gotcha, I am sorta beginning to understand a little. I will read more try to understand more in depth. thank you for your explanation and time. Much appreciated :) – Prab Oct 11 '22 at 17:33

0 Answers0