473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Class Help

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:
class Xyz: .... def y(self):
.... q = 2
....Xyz.y()

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/g...ave/direct/01/

Oct 1 '05 #1
6 1457
Ivan Shevanski wrote:
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:
class Xyz:
... def y(self):
... q = 2
...
Xyz.y()


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



Oct 1 '05 #2
On Sat, 2005-10-01 at 18:58 -0400, Ivan Shevanski wrote:
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:
class Xyz: ... def y(self):
... q = 2
...Xyz.y() 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.
jane

<__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

Oct 1 '05 #3
On Sat, 2005-10-01 at 18:58 -0400, Ivan Shevanski wrote:
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:
class Xyz: ... def y(self):
... q = 2
...Xyz.y() 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.
jane

<__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

Oct 1 '05 #4
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.

Ivan Shevanski wrote:
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:
class Xyz:
... def y(self):
... q = 2
...
Xyz.y()


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/g...ave/direct/01/

Oct 1 '05 #5
Ron Adam wrote:
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
Oct 1 '05 #6
On Sat, 01 Oct 2005 18:58:45 -0400, Ivan Shevanski wrote:
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.
--
Steven.
Oct 2 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Goodyear | last post by:
At the moment im experimenting with ideas in C++ and would really like to solve the following, please please help. Sorry i dont even know what the subject is this would come under? :( Sorry if...
1
by: Naren | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote: >Naren wrote: >> MyServer class conatins a list of Mysock class. >> >> class Myserver >> { >> private: >> list<Mysock> L1; >> }; >>
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
4
by: Mike | last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am trying to implement a singleton class. Form 1 should create a user object and populate some properties in user. Form2...
5
by: Jeff Bunting | last post by:
I'm trying to serialize a class I have and have been getting: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll There was an error reflecting type...
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
11
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class...
4
by: Pupeno | last post by:
Hello, I want to jump over a method in the class hierarchy, that is: If I have class A(object), clas B(A), class C(B) and in C, I want a method to do exactly what A does but not what B does in...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.