Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python classes
#1
Sad 
Hi People

I have some difficulty with classes in Python.
I want to call a method, belonging to class A, from class B. I looked at online examples and tested them successfully, but my code generates an error which I do not understand. I will attach the .py file for details, but in essence here is what I'm trying to do:
class A:
   def __init__(self,a,b):
      self.a_ = a
      self.b_ = b
   
  def set_a(self,aa):
     self.a_ = aa

class B:
   def __init__(self,c):
      self.c_ = c
   
    def set_a_of_classA(self,x):
       A.set_a(self,x)

def main():
   a = A(1,2)
   b = B(7)

   b.set_a_of_classA(10)
The code raises this error:
Error:
AttributeError: 'b' object has no attribute 'a_'
Yoriz write Nov-08-2022, 03:13 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   agent5.py (Size: 3.36 KB / Downloads: 68)
Reply
#2
Please wrap code in Python tags.

Class B is class A with extra stuff. B knows now to set_a. B inherited .set_a() from class A. There is no need for set_a_of_classA(). Just call b.set_a(value)
def set_a_of_classA(self,x):
    """There is no reason for this method to exist.  Call set_a() instead."""
    # But this is how it would be written
    self.set_a(self, x)
This is an error.
class B:
def __init__(self, c):
    self.c_ = c
You are not calling the __init__ method for class A. Attributes a and b are initialized in A.__init__(). Your code should look like this:
def __init__(self, a, b, c):
    super().__init__(a, b)
    self.c_ = c
Reply
#3
Hi deanhystad

Is inheritance the only way to achieve this?
I do not want to implement this via inheritance, because the 2 classes may not always be related.
Let me explain. I'm trying to write a simple table driven agent program (vacuum cleaner cleaning 2 rooms - the classic AI example). In this program the vacuum cleaner must set the room status to "clean" after cleaning it.
Thus, the vacuum object must call the room object's room.set_status('c').

In this scenario a room and a vacuum is not the same type of object and thus implementing inheritance between the room and vacuum classes does not make sense, not so?

Is there no other way to achieve this goal?
Reply
#4
class A:
    def method(self):
        return 42


class B:
    def __init__(self, a_instance):
        self.a = a_instance


b = B(A())
print(b.a.method())
Or you can instanciate the class A directly in the __init__ method.
class A:
    def method(self):
        return 42


class C:
    def __init__(self):
        self.a = A()


print(C().a.method())
Method get_a from instance C calls method from instance A:
class A:
    def method(self):
        return 42


class C:
    def __init__(self):
        self.a = A()
    def get_a(self):
        return self.a.method()

print(C().get_a())
I prefer the composition pattern.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Achieve what goal?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 550 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,210 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,945 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Understanding Python super() for classes OmegaRed94 1 1,852 Jun-09-2021, 09:02 AM
Last Post: buran
  Python classes Python_User 15 4,940 Aug-04-2020, 06:57 PM
Last Post: Python_User
  Python Classes leodavinci1990 1 2,100 Nov-27-2019, 07:25 AM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 5,087 Nov-01-2017, 04:20 PM
Last Post: metulburr
  use of classes in python Lux 2 3,540 Aug-19-2017, 12:29 PM
Last Post: hbknjr
  python classes prsdll6 14 7,456 Aug-17-2017, 07:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020