Class Help

I

Ivan Shevanski

To continue with my previous problems, now I'm trying out classes. But I
have a problem (which I bet is easily solveable) that I really don't get.
The numerous tutorials I've looked at just confsed me.For intance:
.... def y(self):
.... q = 2
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method y() must be called with Xyz instance as first
argument
(got nothing instead)


So. . .What do I have to do? I know this is an extremley noob question but I
think maybe if a person explained it to me I would finally get it =/


thanks in advance,

-Ivan

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
 
R

Ron Adam

Ivan said:
To continue with my previous problems, now I'm trying out classes. But
I have a problem (which I bet is easily solveable) that I really don't
get. The numerous tutorials I've looked at just confsed me.For intance:


... def y(self):
... q = 2
...


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method y() must be called with Xyz instance as first
argument
(got nothing instead)


So. . .What do I have to do? I know this is an extremley noob question
but I think maybe if a person explained it to me I would finally get it =/


thanks in advance,

-Ivan

Generally you don't use class's directly. Think if them as templates
for objects. Then you can use that class (template) to create many objects.

To create an object just call the class and assign the result to a name.

xyz = Xyz()
xyz.y()


Also,

In your example 'q' is assigned the value 2, but as soon as the method
'y' exits, it is lost. To keep it around you want to assign it to self.y.

class Xyz(object): # create an class to create an object instance.
def y(self)
self.q = 2

xyz = Xyz()
xyz.y()
print xyz.q # prints 2



Cheers,
Ron
 
?

=?ISO-8859-1?Q?Jean-Fran=E7ois_Doyon?=

You have to crate an instanciation of the class before you can use one.

So you want to do:

instance = Xyz()
instance.y()

You won't get any output though, might want to do:

class Xyz:
def y(self):
print 'y worked!'

it's more satisfying :)

Basically, look into the difference between a class, and the INSTANCE of
a class.

Cheers,
J.F.
 
M

marduk

To continue with my previous problems, now I'm trying out classes. But I
have a problem (which I bet is easily solveable) that I really don't get.
The numerous tutorials I've looked at just confsed me.For intance:

... def y(self):
... q = 2
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method y() must be called with Xyz instance as first
argument
(got nothing instead)


So. . .What do I have to do? I know this is an extremley noob question but I
think maybe if a person explained it to me I would finally get it =/


When you define a class, say Xyz, your are defining your own type. Say
that Person is a class. And person has a method walk():

class Person:
def walk(self):
...

now to use the Person class, you need to create an instance of it. You
can't just say Person.walk() because Person is a "class"ification, not a
real object. You need an instance of person.

jane = Person()

This creates a new person called "jane". "jane" is an instance of the
class Person.
<__main__.Person instance at 0x2aaaac723710>

Now we can tell jane to walk:

jane.walk()

So what the error is telling you (in a bit confusing way if you're a
newbie) is that you are calling a method y() but you have not created an
instance of your Xyz class to do y(). Or, to use my analogy, you are
telling Person to walk, but you can't make Person walk, you have to
create a person, jane, and have jane walk.

Hope this helps, but I recommend you read a good intro to
object-oriented programming.

-a
 
M

marduk

To continue with my previous problems, now I'm trying out classes. But I
have a problem (which I bet is easily solveable) that I really don't get.
The numerous tutorials I've looked at just confsed me.For intance:

... def y(self):
... q = 2
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method y() must be called with Xyz instance as first
argument
(got nothing instead)


So. . .What do I have to do? I know this is an extremley noob question but I
think maybe if a person explained it to me I would finally get it =/


When you define a class, say Xyz, your are defining your own type. Say
that Person is a class. And person has a method walk():

class Person:
def walk(self):
...

now to use the Person class, you need to create an instance of it. You
can't just say Person.walk() because Person is a "class"ification, not a
real object. You need an instance of person.

jane = Person()

This creates a new person called "jane". "jane" is an instance of the
class Person.
<__main__.Person instance at 0x2aaaac723710>

Now we can tell jane to walk:

jane.walk()

So what the error is telling you (in a bit confusing way if you're a
newbie) is that you are calling a method y() but you have not created an
instance of your Xyz class to do y(). Or, to use my analogy, you are
telling Person to walk, but you can't make Person walk, you have to
create a person, jane, and have jane walk.

Hope this helps, but I recommend you read a good intro to
object-oriented programming.

-a
 
R

Ron Adam

Ron said:
Also,

In your example 'q' is assigned the value 2, but as soon as the method
'y' exits, it is lost. To keep it around you want to assign it to self.y.

Ooops, That should say ...
"To keep it around you want to assign it to self.q." <---self.q

Cheers,
Ron
 
S

Steven D'Aprano

To continue with my previous problems, now I'm trying out classes. But I
have a problem (which I bet is easily solveable) that I really don't get.
The numerous tutorials I've looked at just confsed me.For intance:

[code snipped]


You have to keep in mind the difference between a class and an instance of
a class. To make an analogy with real life, a class is like the idea of
"dogs in general" and an instance is a specific dog (like Ol' Yella, or
Rin Tin Tin, or or that little beagle on the Enterprise).

Normally you create a class, then make one or more instance of the class,
and work with the instances.

Some terminology for you to learn: when you create a function inside a
class, it is called a class method, or just method. Functions and methods
are not exactly the same, but for now the differences don't concern us.

So, you create a class with a single method:

class Klass:
def spam(self):
return "spam " * 5

Notice that the first argument of the method is always "self", even when
you don't need any arguments.

Klass is an object, and you can call Klass.spam() if you like, but it will
fail because you haven't included an argument for self. self must be an
instance of Klass. So you could do this:

spam_maker = Klass() # create an instance
print Klass.spam(spam_maker)

which will print "spam spam spam " as expected.

But that's doing extra work. Once you have your instance, you can just
call the method as if it were a normal function:

print spam_maker.spam()

and it will work the way you expect it to. Behind the scenes, Python
passes a copy of spam_maker to spam_maker.spam() for you. It can do that
because spam_maker is an instance.

A class is a description of how a type of object should work, but you
normally don't work directly on that high-level description. Normally you
will work with individual instances, not the class itself. When Timmy
falls down the well, you tell Rin Tin Tin to go get help, not "dogs in
the general".

Python built in objects like lists, strings, ints etc are special types of
classes built into the language. Here is how we might create a (very
inefficient!) Python implementation of list:

class MyList:

# Class attributes:

left_delimiter = "["
right_delimiter = "]"
item_delimiter = ", "

# Class methods:

def __init__(self, *arguments):
"""Create a new instance and fill it with arguments."""
self.data = arguments # create an instance attribute
def __str__(self):
"""Convert instance to a string for printing."""
holder = self.left_delimiter
for item in self.data:
holder = holder + str(item) + self.item_delimiter
holder = holder + self.right_delimiter
return holder
def append(self, obj):
"""Append an object to the instance."""
self.data.append(obj)
# real lists have many more methods...



Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

gmail access 1
python rounding problem. 15
Help with syntax warnings 6
noob question Letters in words? 8
open4 2
Lines of Strings 4
PHP in instant rails 1
defining classes 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top