Newbie with some doubts.

  • Thread starter Edgar A. Rodriguez
  • Start date
E

Edgar A. Rodriguez

Hi everybody,

Im newbie to Python (I found it three weeks ago) , in fact Im newbie to
programming. I'm being reading and training with the language, but I
still wondering about what Classes are used to. Could you please give
me some examples??

Thanks.
 
B

BartlebyScrivener

Try this.

http://www.ibiblio.org/g2swap/byteofpython/read/oops.html

Best thing is to do a little reading, then wait until you have a
problem or a project that would provide the occasion to create your
first class. Then try it, and if it doesn't work, post it here with (1)
what you were trying to do; (2) the code you used; (3) the result,
including error messages that you got. Also, it usually helps to search
the group first, or even do a google search before posting.

</rpd>
 
C

Chris Lasher

Learning Python by Ascher and Lutz has a very good introduction to
objects and object-oriented programming. If you're new to programming,
I definitely recommend the text. Also, check out Mark Pilgrim's chapter
on OOP in Dive Into Python at
http://www.diveintopython.org/object_oriented_framework/index.html
(Incidentally, Dive Into Python is my favorite programming book,
tremendously well written, and free to read online at
http://www.diveintopython.org. I loved the book so much I went out to
the bookstore and bought a copy.)
 
T

Terry Hancock

Im newbie to Python (I found it three weeks ago) , in fact
Im newbie to programming. I'm being reading and training
with the language, but I still wondering about what
Classes are used to. Could you please give me some
examples??

When you imagine a procedural program, you imagine the
computer (the object) following a list of instructions.

When you write an object-oriented program, you are imagining
a model of a more complex imaginary machine with many
interacting objects that does what you want. The parts of
the machine are "class instances" in Python, and each *type*
of part is a "class". You will also use "classes" and
"class instances" to model data that the program handles.

An object-oriented program is something like a simulation.

This is really useful for programs that do more than just
chug along on a piece of data until they start (which is
sort of the visual model that most people had in mind when
procedural programs were in ascendence). A program that you
interact with needs to act more flexibly, and evolve as you
work with it. It also needs to compartmentalize separate
pieces of functionality so that they don't interfere with
each other.

We call these things "class instances" in Python instead of
"objects", because "object" applies to *all data* in
Python, even simple things like the number '1'. Class
instances are specifically objects defined as a member of a
class.
 
C

Claudio Grondi

Edgar said:
Hi everybody,

Im newbie to Python (I found it three weeks ago) , in fact Im newbie to
programming. I'm being reading and training with the language, but I
still wondering about what Classes are used to. Could you please give
me some examples??

Thanks.
I don't know any really good examples which were able to demonstrate
what Classes are good for and I am in programming already for decades.
There is actually no real need for usage of Classes. The notion, that
they could be useful comes eventually with longer programming experience
and/or with growing size of the code library. If you can avoid to learn
about them, at least at the beginning, take the chance - it will save
you much trouble and help to get things done. I for myself try to avoid
classes where I can, especially inheritance, because I consider the
latter in most cases evil. There are sure many others who can't imagin
to program without classes, so don't conclude that there is a
contradiction here - it's just the question of taste and not of
necessity. I suggest, you learn as much as you can about a list and a
dictionary first and if you have got the idea how to use the latter, you
have learned more or less also what a Class is.

Claudio
 
M

Mike Meyer

As far as I'm concerned, the definitive work in this area is Meyer's
"Object Oriented Software Construction". He covers pretty much all the
uses of OO language features, using a language that was designed
specifically to support those uses. Be warned that after reading it,
you're liable to come back to Python and wonder "Why doesn't Python do
X".

For a good overview of a most programming paradigms, check out
"Concepts, Techniques, and Models of Computer Programming" by Van Roy
and Haridi. He covers OO programming, but not in as much depth as
Meyer. But you'll see how it fits in with other paradigms.
There is actually no real need for usage of Classes.

Well, there's actually no real need for usage of namespaces,
functions, modules, and a host of other things one finds in modern
programming languages, because all those languages are turing
complete. But using those features makes it easier to write, read, and
maintain programs - that's why the languages have them.

If your methods never use the self variable, or you never create more
than one instance of a class, then there's a good chance your program
would be easier to read if it didn't use classes. On the other hand,
if you need multiple instances of some object, then an OO version of
the program is often easier to read than the alternatives.
I for myself try to avoid classes where I can, especially
inheritance, because I consider the latter in most cases evil.

Well, if you can do the job as well without classes or inheritance (or
multiple inheritance, which some language designers dislike enough
that they leave it out), then I'd say do it that way, because those
are powerful mechanisms. More powerful mechanisms generally take more
work to understand, so if a simpler mechanism - which should be easier
to understand - can do the job, you should prefer it.

On the other hand, maintaining and extending the code is often part of
the job. Using classes and inheritance make this part of the job much
easier. For instance, I need to pickle builtin types that the provided
pickle modules can't handle. If they hadn't used classes, or I chose
not to use inheritance, I'd wind up working on that module - or a copy
of it - and patching it to do what I want. Instead, I'm inheriting
from the pickle modules classes, and extending them. This won't be a
lot less work now. But when I upgrade my python installation, that
changes. If I used "cut-n-paste" extension, my choices would be to use
my version of the module, thus miss out on any bug fixes or
enhancements that are in the new version of the module. Or I can try
and port my patches into the new version, which could well be a lot of
work. Since the pickle module uses classes and I inherited from them,
upgrading Python means I automatically get any fixes and enhancements
to that module. Since I'm only using the public interface, things
should just simply work, with no effort on my part. They may not - so
I need to rerun my tests. But I should do that after upgrading Python,
even if I keep using the old module.

<mike
 
C

Claudio Grondi

Mike said:
As far as I'm concerned, the definitive work in this area is Meyer's
"Object Oriented Software Construction". He covers pretty much all the
uses of OO language features, using a language that was designed
specifically to support those uses. Be warned that after reading it,
you're liable to come back to Python and wonder "Why doesn't Python do
X".

For a good overview of a most programming paradigms, check out
"Concepts, Techniques, and Models of Computer Programming" by Van Roy
and Haridi. He covers OO programming, but not in as much depth as
Meyer. But you'll see how it fits in with other paradigms.




Well, there's actually no real need for usage of namespaces,
functions, modules, and a host of other things one finds in modern
programming languages, because all those languages are turing
complete. But using those features makes it easier to write, read, and
maintain programs - that's why the languages have them.

If your methods never use the self variable, or you never create more
than one instance of a class, then there's a good chance your program
would be easier to read if it didn't use classes. On the other hand,
if you need multiple instances of some object, then an OO version of
the program is often easier to read than the alternatives.




Well, if you can do the job as well without classes or inheritance (or
multiple inheritance, which some language designers dislike enough
that they leave it out), then I'd say do it that way, because those
are powerful mechanisms. More powerful mechanisms generally take more
work to understand, so if a simpler mechanism - which should be easier
to understand - can do the job, you should prefer it.

On the other hand, maintaining and extending the code is often part of
the job. Using classes and inheritance make this part of the job much
easier. For instance, I need to pickle builtin types that the provided
pickle modules can't handle. If they hadn't used classes, or I chose
not to use inheritance, I'd wind up working on that module - or a copy
of it - and patching it to do what I want. Instead, I'm inheriting
from the pickle modules classes, and extending them. This won't be a
lot less work now. But when I upgrade my python installation, that
changes. If I used "cut-n-paste" extension, my choices would be to use
my version of the module, thus miss out on any bug fixes or
enhancements that are in the new version of the module. Or I can try
and port my patches into the new version, which could well be a lot of
work. Since the pickle module uses classes and I inherited from them,
upgrading Python means I automatically get any fixes and enhancements
to that module. Since I'm only using the public interface, things
should just simply work, with no effort on my part. They may not - so
I need to rerun my tests. But I should do that after upgrading Python,
even if I keep using the old module.

<mike
Yes, I see your point, but even putting my personal preferences beside,
for someone who just started to program, learning about the concept of
classes and inheritance is probably not what helps to get immediate fun
out of first steps in writing small programs, right?

Claudio
 
M

Mike Meyer

Claudio Grondi said:
Yes, I see your point, but even putting my personal preferences
beside, for someone who just started to program, learning about the
concept of classes and inheritance is probably not what helps to get
immediate fun out of first steps in writing small programs, right?

Right. But after that first 10 minutes, the answer depends on what
they want to do. One of the nice things about Python is that it
doesn't insist that everything be an object, so you can do significant
work without having to know how to write a class, and some people get
by that way. One of the other nice things is that it provides lots of
high-powered tools like classes with multiple inheritance and
metaclasses, and these are everday tools to some people.

Until you know where on that spectrum someone falls, I think it's
inappropriate to try and tell them what tools they will or won't
need. Just tell them where they can find the information they asked
for, and wait until they ask "why" instead of "what" to deal whether
that issue.

<mike
 
K

Kent Johnson

Edgar said:
Hi everybody,

Im newbie to Python (I found it three weeks ago) , in fact Im newbie to
programming. I'm being reading and training with the language, but I
still wondering about what Classes are used to. Could you please give
me some examples??

This essay gives some simple suggestions of when and why you might want
to use classes:
http://www.pycs.net/users/0000323/stories/15.html

Kent
 
J

Jorgen Grahn

As far as I'm concerned, the definitive work in this area is Meyer's
"Object Oriented Software Construction". He covers pretty much all the
uses of OO language features, using a language that was designed
specifically to support those uses.

Bjarne Stroustrup recommends it, but notes "Tends to confuse Eiffel with
universal principles."
Be warned that after reading it,
you're liable to come back to Python and wonder "Why doesn't Python do
X".

Meyer is too much of a fundamentalist for me, so much of the book just
pisses me off. I expect that many Python programmers would feel the same
way, Python being the way it is.

It's still worth reading though -- especially the part on design
by contract. And all his vicious attacks on things you happen to
dislike, too ;-)

/Jorgen
 
M

Mike Meyer

Jorgen Grahn said:
Bjarne Stroustrup recommends it, but notes "Tends to confuse Eiffel with
universal principles."

He does tend to write like the Eiffel way of doing OO is the only
valid way of doing OO.
Meyer is too much of a fundamentalist for me, so much of the book just
pisses me off. I expect that many Python programmers would feel the same
way, Python being the way it is.

I should note that "Python isn't Eiffel" is often a good answer to the
questions "Why doesn't Python do X" that arise from that book. The
design goals of Eiffel are different from the design goals of
Python. I like the design goals of both languages, so I like both
languages.

<mike
 
B

Bruno Desthuilliers

Edgar A. Rodriguez a écrit :
Hi everybody,

Im newbie to Python (I found it three weeks ago) , in fact Im newbie to
programming. I'm being reading and training with the language, but I
still wondering about what Classes are used to.

A class is the definition of a type of object, and let you add your own
object types to the language.

In fact, as soon as you're programming in Python, you are using classes
and objects, even if you're not aware of it. *Everything* in Python is
an object (ie: an 'instance' of a class). The string "foo" is an
instance of class string, the number 42 is an instance of class int,
etc. Even functions are objects (instances of class function, yes), so
you can access (and modify) the properties of a function:

def fun():
"a dummy function"
return "foo"

fun.___doc__
=>"a dummy function"
fun.__class__
Could you please give
me some examples??

Let's go with the good ole (and very dumb) example of a shapes drawing
program.

# The procedural (ie: no classes) version:

def draw_line(from, to):
# code here

def draw_square(origin, width):
# code here

def draw_circle(center, radius):
# code here


shapes = [
{'type': 'square',
'origin' : (10,10),
'width' : 20},
{'type': 'circle',
'center': (40, 40),
'radius': 10},
{'type', 'line',
'origin' : (42,42)
'end' : (48,84)}
]

def draw_shape(shapes):
for s in shapes:
if s['type'] == 'line':
draw_line(s['origin'], s['end'])
elif s['type'] == 'square':
draw_square(s['origin'], s['width'])
elif s['type'] == 'circle':
draw_circle(s['center'], s['radius'])
else:
raise TypeError, "item %s is not a valid shape" % s



# the ObjectOriented version :

class Line(object):
def __init__(self, origin, end):
self.origin = origin
self.end = end

def draw(self):
# code here

class Circle(object):
def __init__(self, center, radius):
self.center = center
self.radius = radius

def draw(self):
# code here

class Square(object):
def __init__(self, origin, width):
self.origin = origin
self.width = width

def draw(self):
# code here


shapes = [
Square((10,10), 20),
Circle((40, 40),10),
Line((42,42), (48,84)),
]

def draw_shapes(shapes):
for s in shapes:
s.draw()



Now try to add to both versions a new type of shape, 'rect', with
origin, width and height. In the first case, you'll need to modify the
draw_shapes() function. In the second case, since each shape type know
how to draw itself (this is called 'encapsulation'), you'll only have to
define the Rect class.

The example being a small and dumb example, the advantage may not be
that obvious, but think of what this would mean for a full-featured
shape drawing program. And there is of course *much* more than this in
OOP...

HTH
 
P

Peter Maas

Claudio said:
Im newbie to Python (I found it three weeks ago) , in fact Im newbie to
programming. I'm being reading and training with the language, but I
still wondering about what Classes are used to. Could you please give
me some examples??
[...]
I don't know any really good examples which were able to demonstrate
what Classes are good for and I am in programming already for decades.

Then you have in all those decades for sure never seen a GUI program
done with the Win32 API (not OO, ugly, hard to grasp) and a Delphi
equivalent (OO, elegant, easy).
There is actually no real need for usage of Classes.

This is your opinion, not a fact as your wording suggests. If the OP
is asking for examples it doesn't make sense to answer "Hey, I don't
know any examples".
> The notion, that they could be useful comes eventually with longer
> programming experience and/or with growing size of the code library.

I don't think so. OO is modeled after human reasoning. Look e.g.
at phrases like Bob calls a friend, Bob learns math.

What is better:

Bob.call(friend) or human_call(Bob, friend), Bob.learn(math) or
human_learn(Bob, math)?

On the other hand there are cases where the emphasis is on the
verb, e.g. compute the sine of alpha: sin(alpha) is perfect,
no need to code alpha.sin(). Therefore I like hybrid languages
like python giving the programmer the freedom to choose an
appropriate model.
> If you can avoid to learn about them, at least at the beginning,
> take the chance - it will save you much trouble and help to get
> things done.

Excuse me, but to present your personal experience as a law of
nature is not very helpful for a newbie seeking for advice not
for propaganda. It is plain wrong to claim that procedural
programming is kind of natural and OOP is rocket science.
I for myself try to avoid classes where I can, especially
> inheritance, because I consider the latter in most cases evil.

There are terrible OO libraries out there but this is not
a genuine feature of OOP. Bad code can be written in lots
of ways.
> There are sure many others who can't imagin to program
> without classes, so don't conclude that there is a
> contradiction here - it's just the question of taste

It's not a question of taste it's a question of the kind
of problem to solve.

Peter Maas, Aachen
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top