Home Why does Python not connect to my class attribute?
Reply: 1

Why does Python not connect to my class attribute?

Aphrodite
1#
Aphrodite Published in 2018-02-14 10:05:08Z

I am trying to make a game similar to a MMORPG but is single player. I am trying to put the Player class on one script, the MaelstromPlayer, and it's child class MaelstromMove to connect to it using another script because I am adding the MaelstromBattle and MaelstromInv classes soon.

The problem is that I could not access, not to mention adjust, the MaelstromPlayer class attributes. I wonder what caused this problem and how to solve it, or even if this is possible.

In case you were wondering, I got the method of "Distributing A Class Across Multiple Files" from this website https://mail.python.org/pipermail/python-list/2012-January/618880.html .

This is my code for MaelstromPlayer, the file where I store the parent class.

class Player(object):
    def __init__(self):
        self.place = int("001")
        self.name = input("What name do you want?")
        self.knownplaces={}
        self.knownplaces[int("001")]="Ruby City"
        self.knownplaces[int("002")]="Ruby Inn"
        self.knownplaces[int("003")]="Ruby Forests"
        self.knownplaces[int("004")]="Ruby Countryside"
        self.knownplaces[int("005")]="Witch Hideout"
        self.mode="moving"
    def __str__(self):
        rep = self.movepossible

This is my code for MaelstromMove, where I store the child class of PlayerMove.

import sys
sys.path.append('F:\Maelstrom\Python\MaelstromPlayer.py')
from MaelstromPlayer import Player

class PlayerMoving(Player):
    def __init__(Player):
        print('Welcome')
    def movepossible(Player,position):
        #001--Ruby City
        #002--Ruby Inn
        #003--Ruby Forests
        #004--Ruby Countryside
        #005--Witch Hideout
        if position==int("001"):
            possible=[int("002"),int("003")]
            return possible
        elif position==int("002"):
            possible=[int("001")]
            return possible
        elif position==int("003"):
            possible=[int("001"),int("004")]
            return possible
        elif position==int("004"):
            possible=[int("001"),int("003"),int("005")]
            return possible
        elif position==int("005"):
            possible=[int("004")]
            return possible
        else:
            return null
    def move(Player,position):
        if Player.mode=="moving":
            position=int(position)
            possiblewords=[]
            print('Choose between paths:')
            possible = Player.movepossible(position)
            for m in range(0,len(possible),1):
                possiblewords.append(Places.knownplaces[possible[m]])
            for n in range(0,len(possiblewords),1):
                print(str(n+1)+':'+str(possiblewords[n]))

            choice=input('Make your choice...')
                #choice=int(choice)
            if choice == '':
                choice = int('9999999999')
            if int(choice) <= len(possiblewords):
                Player.place=possible[int(choice)-int('1')]
            print("\n")
            print("\n")
    def showposition(Player):
        print('You are at '+Player.knownplaces[int(Player.place)])

test = Player()
while True:
    place = test.place
    test.move(place)
    test.showposition()

The sys.path method is from here: http://www.daveoncode.com/2017/03/07/how-to-solve-python-modulenotfound-no-module-named-import-error/ .

Please help, preferably with a code sample, thank you.

FHTMitchell
2#
FHTMitchell Reply to 2018-02-14 10:39:14Z

Your problem is that you never call the the PlayerMoving class (for your test at the bottom), just the Player class. Also in the PlayerMoving class you override the Player class' __init__ method. To make sure the Player.__init__ is still called, do something like this:

class PlayerMoving(Player):
    def __init__(self):
        print('Welcome')
        super().__init__()

Some additional problems:

  • don't use int("001"), it's inefficient, pointless and just bad practice.
  • For creating your dict do self.knownplaces = {1: "Ruby City", 2: "Ruby Inn", ...} # etc
  • null isn't defined in python, perhaps you mean None but you'll automatically return None if you don't return anything else.
  • Your while loop will never end. Put a break clause in or something.
  • Don't use Player as the first method argument. Use self like you'll find in every python class example ever 12345.

Finally, have you noticed that that entire thread from https://mail.python.org/pipermail/python-list/2012-January/618880.html is about how you should never do this?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35019 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO