Python Forum
calling on a method from one class into another class which is not a child
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling on a method from one class into another class which is not a child
#1
I have a class called World, another one called Fish. Fish is not a child of World, and I want to call the method getMaxX() and getMaxY() into the Fish class, but I get attribute errors. How Can I Call getMaxX() and getMaxY() from World class into Fish class?

class World:
    def __init__(self, mX, mY):
        self.__maxX = mX
        self.__maxY = mY
        self.__thingList = []
        self.__grid = []
.....

    def getMaxX(self):
        return self.__maxX

    def getMaxY(self):
        return self.__maxY


#Now here is the Fish class

class Fish(LifeForm):
    def __init__(self):
        super().__init__("Fish.gif")
        
        self.__breedTick = 0
        self.__starveTick = 0
   ...

    def liveALittle(self):
        offsetList = [(-1,1), (0,1), (1,1),
                      (-1,0),        (1,0),
                      (-1,-1),(0,-1),(1,-1)]
        adjFish = 0  #count adjacent Fish
        for offset in offsetList:
            newX = self.getX() + offset[0]
            newY = self.getY() + offset[1]
            if 0 <= newX < ??????????????? and \
                  0 <= newY <????????????????????):
                if (not World.emptyLocation(newX, newY)) and \
                    isinstance(World.lookAtLocation(newX, newY), Fish):
                    adjFish = adjFish + 1

        if adjFish >= 2:   #if 2 or more adjacent Fish, die
            World.delThing()
        else:
            self.__breedTick = self.__breedTick + 1
            if self.__breedTick >= 12:  #if alive 12 or more ticks, breed
                self.tryToBreed()

...


What should go there instead of ??? I tried World.getMaxX(), self.__world.getMaxX(), etc. Nothing worked. I'm getting an attribute error saying the Fish doesn't have any world attribute or the missing positional argument:self error.
Reply


Messages In This Thread
calling on a method from one class into another class which is not a child - by NABA - Apr-29-2020, 06:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class Blockage not projecting Azdaghost 1 500 May-15-2025, 04:32 PM
Last Post: deanhystad
  Return a string or byte object from Enum class? Calab 5 844 May-14-2025, 05:21 PM
Last Post: snippsat
  Python inner classes inheritance from parent class Abedin 8 1,424 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 1,442 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,590 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,716 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,185 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 4,347 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  [split] Class and methods ebn852_pan 15 3,820 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,461 May-13-2024, 05:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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