7 Replies - 421 Views - Last Post: 01 February 2013 - 02:17 PM Rate Topic: -----

#1 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Calling Class Error: The Sequel

Posted 30 January 2013 - 03:37 PM

Wasn't sure if to continue with the old thread since it's vaguely similar in terms of what the problem is; unlike my last problem where I had multiple classes within a single file, I've had to split my code into a few separate files for sake of brevity/debugging/etc.

Anyhow... once I execute say a particular file that imports from another and try to call a few methods from that class, I get something like this:
##running Loader.py

>>> x = vectors()
>>> x.load_Pos()

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    x.load_Pos()
  File "C:\Python\workbench\2d_astro\Loader.py", line 25, in load_Pos
    Operator().set_posX(pos_x)
TypeError: 'module' object is not callable



The code looks something like this:
#Loader.py
import Operator

class vectors(object):
    def load_Pos(self, pos_x = 0, pos_y = 0, pos_z = 0):
        Operator().set_posX(pos_x)
        Operator().set_posY(pos_y)
        Operator().set_posZ(pos_z)

        vector = [Operator().get_posX(),
                  Operator().get_posY(),
                  Operator().get_posZ()]
        
        return vector


Which is calling from this:
#Operator.py
import math

class functions(object):

    def set_posX(self, pos = 0):
        self.pos_x = pos

    def get_posX(self):
        return self.pos_x



I'm would be almost certain that I'm not typing out the proper syntactical form for calling other classes (in other files) or something, but I can't figure out exactly how python wants it to be as "input".

This post has been edited by BloodyInari: 30 January 2013 - 03:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calling Class Error: The Sequel

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Calling Class Error: The Sequel

Posted 30 January 2013 - 04:06 PM

You are importing Operator, which is a module. Operator is not a class or function so you can't call it uses brackets ().

functions is a separate class, so you might choose to instantiate an object of this class:

some_fun = functions()

and then you might call the methods set_posX and get_posX on this instance (of the class functions). There is no connection between set/get_posX and your other class, vectors.

This post has been edited by andrewsw: 30 January 2013 - 04:07 PM

Was This Post Helpful? 1
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Calling Class Error: The Sequel

Posted 30 January 2013 - 05:12 PM

Note that there is a built in module "operator" what can make your program a bit confusing. Maybe a different name for the file would be more suitable.
Was This Post Helpful? 0
  • +
  • -

#4 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Calling Class Error: The Sequel

Posted 31 January 2013 - 01:14 PM

View Postandrewsw, on 30 January 2013 - 05:06 PM, said:

You are importing Operator, which is a module. Operator is not a class or function so you can't call it uses brackets ().


Thanks for the tip, I was pretty sure that was one of the problems involved.

View Postandrewsw, on 30 January 2013 - 05:06 PM, said:

some_fun = functions()

and then you might call the methods set_posX and get_posX on this instance (of the class functions). There is no connection between set/get_posX and your other class, vectors.


So, just off the top of my head, something like this will work then?
class vectors(object):

    def __init__(self):
        
        self.function = Equations.library()

    def load_Pos(self, pos_x = 0):

        self.function.set_posX(pos_x)
        vector = [self.function.get_posX()]
        self.pos_vector = vector


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Calling Class Error: The Sequel

Posted 31 January 2013 - 04:03 PM

Erm, dunno, as you haven't detailed where this comes from:

Equations.library()

Was This Post Helpful? 0
  • +
  • -

#6 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Calling Class Error: The Sequel

Posted 31 January 2013 - 05:26 PM

View Postandrewsw, on 31 January 2013 - 05:03 PM, said:

Erm, dunno, as you haven't detailed where this comes from:

Equations.library()


Uh sorry, just ignore it; it's effectively the Operator module and class.
Anyhow, tinkered around some more, it works about right but I have another question.

As it now tries to get/set data from one class to another, but the problem is as such that it doesn't seem to perform the proper operations in one class to give any data to the one that's calling it.

for example, if this is what is executed in the shell, it won't result in an error and appear as though everything has gone accordingly but when you try to access the variable/array list for which the data is stored:
>>> x = vectors()

>>> x.load_Pos()
>>> x.pos_vector
[<bound method main.pos_x of <Equations.main object at 0x02AB54F0>>, <bound method main.pos_y of <Equations.main object at 0x02AB54F0>>, <bound method main.pos_z of <Equations.main object at 0x02AB54F0>>]



however, I stilled fiddled with it and got this output as well. Why is that?
>>> x = vectors()
>>> y = Equations.main()

>>> y.set_posX(0)
>>> y.set_posY(0)
>>> y.set_posZ(0)
>>> position = [y.get_posX(), y.get_posZ(), y.get_posZ()]
>>> position

[0, 0, 0]



[Edit: ]If it appears in any way to be confusing, I've included the pertinent bits below.
Spoiler

Spoiler

This post has been edited by BloodyInari: 31 January 2013 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Calling Class Error: The Sequel

Posted 31 January 2013 - 07:24 PM

What are you expecting to happen(?).

In the first example you haven't set any positions and Python does its best to output the (empty) attribute pos_vector.

In the second example it does what you've asked it to - although 0,0,0 are a poor choice for testing purposes :whistling:/>

BTW For this second example, your x and y are totally unrelated - I don't know if you were expecting them to be connected in some way.

I get the impression that main() should be a nested/inner class of vectors(), but it depends on what you are trying to achieve.

BTWW Class names should begin with a capital letter and are most often singular.

This post has been edited by andrewsw: 31 January 2013 - 07:24 PM

Was This Post Helpful? 1
  • +
  • -

#8 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Calling Class Error: The Sequel

Posted 01 February 2013 - 02:17 PM

View Postandrewsw, on 31 January 2013 - 08:24 PM, said:

What are you expecting to happen(?).


In the first example you haven't set any positions and Python does its best to output the (empty) attribute pos_vector.


BTW For this second example, your x and y are totally unrelated - I don't know if you were expecting them to be connected in some way.


Gotcha. Fixed with a try/except catch.
    def set_posX(self, pos = 0):
        self.ifBool(pos)
        self.pos_x = pos

    def get_posX(self):
        try:
            return self.pos_x
        except AttributeError:
            self.set_posX()
            return self.pos_X


I suppose it might be bad coding practice but in most of the cases for my variables, if they haven't been defined, need to be designated along the origin of my coordinate system with null (aherm, "zero") values. Hence [0,0,0] as an output for my examples.
That's why in the first example I was expecting some set of default numbers to at least be returned when load_Pos() was called.

View Postandrewsw, on 31 January 2013 - 08:24 PM, said:

I get the impression that main() should be a nested/inner class of vectors(), but it depends on what you are trying to achieve.


Ehhh... it could work that way (actually that was how it originally was set up...) but my Equations class is already fairly large as it is and uses a lot of different functions that are not necessarily tied in to/with my Loader class; thus the reason they are separate files.
Secondly, I'm trying to get more exposure and understanding on using multiple classes in relation to one another while trying to break my single-file-project habits. :sweatdrop:

This post has been edited by BloodyInari: 01 February 2013 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1