Author Topic: I have half broken Python Code  (Read 2813 times)

0 Members and 1 Guest are viewing this topic.

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
I have half broken Python Code
« on: October 09, 2013, 01:43:46 am »
Here is the current state of my half broken python code for a uni assignment. I am having the most worries/errors about trying to call variables between classes.
Thinking possibly to change
class MazeApp(Frame): 

class MazeApp(Object):
so I can call objects, but it's giving me errors such as:

TypeError: unbound method get_size() must be called with Maze instance as first argument (got nothing instead)
TypeError: unbound method make_maze() must be called with MazeGenerator instance as first argument (got str instance instead)

I don't know what to expect.

There is a assign2.py file which is the main code, and mazegenerator.py which is support code given (not to be changed). My work is in the assign2.py file.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline TravisE

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 182
  • Rating: +33/-0
    • View Profile
    • ticalc.org
Re: I have half broken Python Code
« Reply #1 on: October 09, 2013, 02:55:19 am »
Edit: Actually, the exception to the below is when you're subclassing another class and call the parent class's method. I just realized you mentioned changing MazeApp to subclass object instead of Frame. This won't work if you are trying to extend/call Frame's methods, because they are no longer available if you're not basing the new class on it.

I don't understand exactly why you want to change it to "class MazeApp(object)", though. This is only necessary (in Python 2) when creating a parent class. Frame most likely eventually subclasses object at some point, so this is unnecessary.

Or, are you referring to trying to subclass a class called 'Object', rather than the built-in class 'object'?

Those errors can be generated when there's an attempt to call a class's methods directly, without first creating an instance and assigning it to a variable. For example, if you have a class like

Code: [Select]
class Foo(object):
    def bar(self):
        print 'blah'

Make sure you're doing something like

Code: [Select]
f = Foo()
f.bar()

and not

Code: [Select]
Foo.bar()
« Last Edit: October 09, 2013, 03:22:45 am by TravisE »
ticalc.org staff member—http://www.ticalc.org/

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: I have half broken Python Code
« Reply #2 on: October 09, 2013, 06:03:37 pm »
Maze __init__ requires 2 arguments (self, string)
and say for instance I wanted to call _get_size() from Maze class in a method of MazeApp class
I've tried Maze._get_size(), Maze()._get_size() but Maze requires 2 arguments (including self), and I can't get that to work.

I don't want to call Maze __init__, I want to call Maze._get_size() but it requires having Maze __init__ anyway.
No self won't work since self will just reference the MazeApp class.


If Maze __init__ was just 1 argument (self), then I could call any method of Maze with Maze().[methodname]()
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline TravisE

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 182
  • Rating: +33/-0
    • View Profile
    • ticalc.org
Re: I have half broken Python Code
« Reply #3 on: October 09, 2013, 08:52:11 pm »
If you're subclassing Maze, then calling Maze._get_size(self, string) from a MazeApp method should be correct. The self in this case does refer to the MazeApp instance, but this is how it works in Python.
« Last Edit: October 09, 2013, 08:52:29 pm by TravisE »
ticalc.org staff member—http://www.ticalc.org/