using a class method outside its class

This is the place for queries that don't fit in any of the other categories.

using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 7:00 pm

this idea was given to me. but i'm not sure hot to use it. can anyone help?

Code: Select all
class Whatever:

    def key_event(self, key, dir):
        if key ==  ...

    def keydown(key):
        .....  < === here ... outside the above class i'm supposed to call this method.  how do i do this?


thanks

edit.. i'm supposed to use the metod defined under Whatever class. ===> def key_event(self, key, dir): INSIDE def keydown(key): ...
Last edited by tomtom on Wed Jun 05, 2013 7:53 pm, edited 3 times in total.
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby metulburr » Wed Jun 05, 2013 7:24 pm

Please use code tags next time

Code: Select all
instance = Whatever()
instance.keydown(None)

Where None is there for whatever key is.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 7:38 pm

i just utilized your suggestion. thank you very much.

i now get this error:

TypeError: __init__() takes exactly 6 arguments (1 given) for

instance = Whatever()

in other tries when i tried to use the class name and call otehr methods.. i tried to insert the parameters/arguments in it. but then the arguments are not defined. then it would take for me to make local variables which i'm not supposed to. i'm supp to just call the method. :(

any ideas?
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby Yoriz » Wed Jun 05, 2013 7:44 pm

Your class that you are actually using has a __init__ method that takes 5 parameters (self uses one of the 6)
Your are not passing it any parameters which is why you are getting the error.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 7:47 pm

right. as i mentioned. i've tried to pass it's arguments but then i get undefined arguments. as i said i'm not supposed to create local variables for these. so i can't add anything extra. i'm supposed to just call the method and be able to make the keys respond and update whatever they are told to do in Whatever() class.
but please let me know an example if you think i'm misunderstanding what you said.


edit... i have edited my original post if it's will help clarify what i'm trying to do.
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby metulburr » Wed Jun 05, 2013 7:54 pm

have you read Class Basics ?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: using a class method outside its class

Postby Yoriz » Wed Jun 05, 2013 8:06 pm

The code you are showing has no __init__ method so if you passed it 5 argument it would say
Code: Select all
TypeError: object() takes no parameters

Show the actual class you are using that has an __init__ method and how you are trying to use it.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 8:06 pm

yes. apparently i'm dense. i understand it. i just can't implement it.
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 8:14 pm

i'll try to show more without being too specific. i don't want to get in trouble.

class Whatever:
def __init__(x, y, z, q, image, info):
self.x= [x[0],x[1]]
self.y= [y[0],y[1]]
... etc

def draw(self, canvas):

.... etc

def update(self):

..... etc

def key_event(self, key, dir):

if key == .....


then... i should have the keyhandler descripbed in my original post that will be defined outside and i'm trying to use the key_event method in it.
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby Yoriz » Wed Jun 05, 2013 8:15 pm

What do you mean by 'get in trouble' ?
I might be being dense as well but i can't really see an actual question in your post about your possible homework.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 8:33 pm

ok.
thank you
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby Crimson King » Wed Jun 05, 2013 10:27 pm

tomtom wrote:i'll try to show more without being too specific. i don't want to get in trouble.

class Whatever:
def __init__(x, y, z, q, image, info):
self.x= [x[0],x[1]]
self.y= [y[0],y[1]]
... etc

def draw(self, canvas):

.... etc

def update(self):

..... etc

def key_event(self, key, dir):

if key == .....


then... i should have the keyhandler descripbed in my original post that will be defined outside and i'm trying to use the key_event method in it.


Hey tomtom, you should post your entire code, we can't help you without reading it all.

One thing i notice though, is that in your def __init__, you're missing 'self' on your argument list. You should change that to def __init__(self,x, y, z, q, image, info).
User avatar
Crimson King
 
Posts: 122
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 10:37 pm

Self is there. This was just so you can see whats going on. From what I've gotten so far I see that yes... you might not be able to help me unless u have the whole thing which I can't post. I have the code completed. I just can't get my keys to respond to the method. It really sucks because what's learning about? If its not showing someone something the. Let them do similar problems. This technique I've never seen yet I'm expected to know what to do.
So thank you anyway for reading my question.
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm

Re: using a class method outside its class

Postby Crimson King » Wed Jun 05, 2013 11:34 pm

tomtom wrote:Self is there. This was just so you can see whats going on. From what I've gotten so far I see that yes... you might not be able to help me unless u have the whole thing which I can't post. I have the code completed. I just can't get my keys to respond to the method. It really sucks because what's learning about? If its not showing someone something the. Let them do similar problems. This technique I've never seen yet I'm expected to know what to do.
So thank you anyway for reading my question.


The thing is we don't know what's going on, i made a suggestion with the code you wrote and now you tell me that the argument is there (how am i supposed to know that?)

Try this then:

Code: Select all
a = whatever(1,2,3,4,image,info)


Take into account that i don't have a clue what image and info are in your __init__ method.

I repeat: If you want precise help you should post your entire code or at least your 'whatever' class.
User avatar
Crimson King
 
Posts: 122
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: using a class method outside its class

Postby tomtom » Wed Jun 05, 2013 11:46 pm

thank you very much for taking the time to respond.

i've tried that. it gives me "1" is not defined. or after changing things i get atribute error. then image and info are not defined. those i couldn't get away from at all. well i know now that it's impossible to get anything without code. i just thought that with my original question.. i could get some ideas on how to implement it. like the first responder did. but that did not work. so maybe really there might be something wrong with my code to start with but test templates are passing my code completely. that's why i was confident my code is ok so far.

sorry to have wasted your times. i'm just frustrated beyond words about this. i am BRAND NEW at this... and was trying everywhere to get some clues.

thank you again really for trying to work with me.

i will update everyone once the deadline passes and i can get someone to actually TELL me how to do this from my own group. maybe it will help the next person. because i really am done with this particular problem.
tomtom
 
Posts: 8
Joined: Wed Jun 05, 2013 6:55 pm


Return to General Coding Help

Who is online

Users browsing this forum: mdovey, snippsat, W3C [Linkcheck] and 2 guests