new to python classes

52 views
Skip to first unread message

Sumant Shenoy

unread,
Jun 1, 2015, 5:16:15 AM6/1/15
to python_in...@googlegroups.com
i am trying to learn classes but i keep getting this errror can any one tell me what am i doing wrong 

 line 10: unbound method info() must be called with test instance as first argument (got nothing instead)
class test(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def info(self):
         print "%s,%s"%(self, self.x, self.y)

ok=test("pSphere1","pSphere2")
 
test.info()    

Justin Israel

unread,
Jun 1, 2015, 5:41:40 AM6/1/15
to python_in...@googlegroups.com

You almost have the whole thing correct, except for how you end up calling the method. A method is "bound" to a specific instance of a class.

# "ok", while not a very descriptive name, is now an instance of class "test"
ok = test("pSphere1", "pSphere2")

# call a method on your instance
ok.info()


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAGhqNnmE5FHP4xtxdF6rgrfSh4B7SFNNHsdmfOOdbYP_0d9%2BUQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Rémi Deletrain

unread,
Jun 1, 2015, 5:41:51 AM6/1/15
to python_in...@googlegroups.com
try this
 
class test(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def info(self):
         print "%s,%s"%(self, self.x, self.y)

ok=test("pSphere1","pSphere2")

if you want call info with no instance add a decorator '@classmethod' and change 'self' by 'cls' before your function

@classmethod
def info(cls):

Justin Israel

unread,
Jun 1, 2015, 5:44:06 AM6/1/15
to python_in...@googlegroups.com

This is an error. "self" is not defined. And if you switch it to "cls" it will still be an error because x and y are instance variables



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/70f3e4b6-0b41-4c5d-820f-5899c6d2646e%40googlegroups.com.

Rémi Deletrain

unread,
Jun 1, 2015, 5:45:24 AM6/1/15
to python_in...@googlegroups.com
oh yes I try after and I see my error... sorry...


--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/jVpv71o1tuk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2RoPibZh9_sRVrxLmJn8LTK%3D9wmLsq3QeZHzJ%3DeY2RmQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Rémi Deletrain.

Sumant Shenoy

unread,
Jun 1, 2015, 6:05:23 AM6/1/15
to python_in...@googlegroups.com
thanks for your quick reply guys :) as i am preety new to classes i am not getting some stuff 
number 1 i only knew thre was some thing cld self and cls is a whole new thing i am guessing clss is short form for class and access class atributes 
i did change but i still got some errors

example 1 
class test(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def info(cls):
        print "%s,%s"%(self, self.x, self.y)

ok=test("pSphere1","pSphere2")
test().info()

i got an error saying 
 __init__() takes exactly 3 arguments (1 given) 


example 2 

class test(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def info(self):
         print "%s,%s"%(self, self.x, self.y)

ok=test("pSphere1","pSphere2")
line 6: not all arguments converted during string formatting
 

Justin Israel

unread,
Jun 1, 2015, 6:18:05 AM6/1/15
to python_in...@googlegroups.com
I think you should disregard the suggestions about "cls" and classmethod. That suggestion, I don't feel, was properly taking into consideration your level of experience, and is only going to serve to confuse you until you get a bit more comfortable with basic class concepts.

So, in the second example, you have one more error. 

def info(self):
    print "%s,%s" % (self, self.x, self.y)

If you count the number of %s elements in your string, you have 2. But you are trying to pass 3 items to the formatting operation. Change it to this:

def info(self):
    print "%s,%s,%s" % (self, self.x, self.y)

That will pretend the string representation of your test class instance, the value of x, and the value of y



Sumant Shenoy

unread,
Jun 1, 2015, 6:34:19 AM6/1/15
to python_in...@googlegroups.com
That worked perfectly :D
Thank you !!!

Justin Israel

unread,
Jun 1, 2015, 6:38:32 AM6/1/15
to python_in...@googlegroups.com
On Mon, Jun 1, 2015 at 10:34 PM Sumant Shenoy <shenro...@gmail.com> wrote:
That worked perfectly :D
Thank you !!!

Very welcome.
 

On Mon, Jun 1, 2015 at 7:17 PM, Justin Israel <justin...@gmail.com> wrote:
I think you should disregard the suggestions about "cls" and classmethod. That suggestion, I don't feel, was properly taking into consideration your level of experience, and is only going to serve to confuse you until you get a bit more comfortable with basic class concepts.

So, in the second example, you have one more error. 

def info(self):
    print "%s,%s" % (self, self.x, self.y)

If you count the number of %s elements in your string, you have 2. But you are trying to pass 3 items to the formatting operation. Change it to this:

def info(self):
    print "%s,%s,%s" % (self, self.x, self.y)

That will pretend the string representation of your test class instance, the value of x, and the value of y

typo: s/pretend/print out
 
Reply all
Reply to author
Forward
0 new messages