Mailing List Archive

Please help me to understand classes
I have been playing around with python for some time now, but I am a
relative newbie as it is my hobby and normally I have very little time for
programming. I am on leave now and want to write a program but I find
python a very difficult language to comprehend despite the fact that I
have a few years of experience in programming a language like Pascal.
Maybe it is because I have little experience in OOP.

Can somebody explain to me why the following program produces no output:
---------------------------------------------
class A:

def druk():
print 'Jy is in Class A'

class B:

def druk():
print 'Jy is in Class B'

a = A()
c = B()
c.druk
a.druk
---------------------------------------------


--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies@futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------

"One thing have I desired of the LORD, that will I seek
after; that I may dwell in the house of the LORD all
the days of my life, to behold the beauty of the LORD,
and to inquire in his temple."
Psalms 27:4
Please help me to understand classes [ In reply to ]
From: "Evan Simpson" <evan@tokenexchange.com>

Your Pascal experience is showing <wink>. While you can call a
parameterless function or procedure without parentheses in Pascal, Python
always requires them, since functions are first-class objects.

So when you write "a.druk", you are fetching the method "druk" from "a", but
then doing nothing with it. You should write "a.druk()", which will call
the method with no parameters.

One reason for the required parentheses is the ability to do this:

f = a.druk # get the method and make f refer to it.
f() # call the method.

Johann Spies wrote in message ...
I have been playing around with python for some time now, but I am a
relative newbie as it is my hobby and normally I have very little time for
programming. I am on leave now and want to write a program but I find
python a very difficult language to comprehend despite the fact that I
have a few years of experience in programming a language like Pascal.
Maybe it is because I have little experience in OOP.

Can somebody explain to me why the following program produces no output:
---------------------------------------------
class A:

def druk():
print 'Jy is in Class A'

class B:

def druk():
print 'Jy is in Class B'

a = A()
c = B()
c.druk
a.druk
Please help me to understand classes [ In reply to ]
From: Johann Spies <jhspies@futurenet.co.za>

I have been playing around with python for some time now, but I am a
relative newbie as it is my hobby and normally I have very little time for
programming. I am on leave now and want to write a program but I find
python a very difficult language to comprehend despite the fact that I
have a few years of experience in programming a language like Pascal.
Maybe it is because I have little experience in OOP.

Can somebody explain to me why the following program produces no output:
---------------------------------------------
class A:

def druk():
print 'Jy is in Class A'

class B:

def druk():
print 'Jy is in Class B'

a = A()
c = B()
c.druk
a.druk
---------------------------------------------


--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies@futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------

"One thing have I desired of the LORD, that will I seek
after; that I may dwell in the house of the LORD all
the days of my life, to behold the beauty of the LORD,
and to inquire in his temple."
Psalms 27:4
Please help me to understand classes [ In reply to ]
Your Pascal experience is showing <wink>. While you can call a
parameterless function or procedure without parentheses in Pascal, Python
always requires them, since functions are first-class objects.

So when you write "a.druk", you are fetching the method "druk" from "a", but
then doing nothing with it. You should write "a.druk()", which will call
the method with no parameters.

One reason for the required parentheses is the ability to do this:

f = a.druk # get the method and make f refer to it.
f() # call the method.

Johann Spies wrote in message ...
I have been playing around with python for some time now, but I am a
relative newbie as it is my hobby and normally I have very little time for
programming. I am on leave now and want to write a program but I find
python a very difficult language to comprehend despite the fact that I
have a few years of experience in programming a language like Pascal.
Maybe it is because I have little experience in OOP.

Can somebody explain to me why the following program produces no output:
---------------------------------------------
class A:

def druk():
print 'Jy is in Class A'

class B:

def druk():
print 'Jy is in Class B'

a = A()
c = B()
c.druk
a.druk
Please help me to understand classes [ In reply to ]
Finally, a simple question I can answer!

First of all, class methods like "druk" need to have a parameter declared in
them called self (or whatever you want to call it). Secondly you need to
call the method with open/close parens:

class A:
def druk(self):
print 'Jy is in Class A'

class B:
def druk(self):
print 'Jy is in Class B'

a = A()
c = B()
c.druk()
Jy is in Class B
a.druk()
Jy is in Class A

I hope this helps,

David.


-----Original Message-----
From: Johann Spies [mailto:jhspies@futurenet.co.za]
Sent: Tuesday, June 29, 1999 2:04 PM
To: Python-poslys
Subject: Please help me to understand classes


I have been playing around with python for some time now, but I am a
relative newbie as it is my hobby and normally I have very little time for
programming. I am on leave now and want to write a program but I find
python a very difficult language to comprehend despite the fact that I
have a few years of experience in programming a language like Pascal.
Maybe it is because I have little experience in OOP.

Can somebody explain to me why the following program produces no output:
---------------------------------------------
class A:

def druk():
print 'Jy is in Class A'

class B:

def druk():
print 'Jy is in Class B'

a = A()
c = B()
c.druk
a.druk
---------------------------------------------


--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies@futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------

"One thing have I desired of the LORD, that will I seek
after; that I may dwell in the house of the LORD all
the days of my life, to behold the beauty of the LORD,
and to inquire in his temple."
Psalms 27:4
Please help me to understand classes [ In reply to ]
From: "Stidolph, David" <stidolph@origin.ea.com>

Finally, a simple question I can answer!

First of all, class methods like "druk" need to have a parameter declared in
them called self (or whatever you want to call it). Secondly you need to
call the method with open/close parens:

class A:
def druk(self):
print 'Jy is in Class A'

class B:
def druk(self):
print 'Jy is in Class B'

a = A()
c = B()
c.druk()
Jy is in Class B
a.druk()
Jy is in Class A

I hope this helps,

David.


-----Original Message-----
From: Johann Spies [mailto:jhspies@futurenet.co.za]
Sent: Tuesday, June 29, 1999 2:04 PM
To: Python-poslys
Subject: Please help me to understand classes


I have been playing around with python for some time now, but I am a
relative newbie as it is my hobby and normally I have very little time for
programming. I am on leave now and want to write a program but I find
python a very difficult language to comprehend despite the fact that I
have a few years of experience in programming a language like Pascal.
Maybe it is because I have little experience in OOP.

Can somebody explain to me why the following program produces no output:
---------------------------------------------
class A:

def druk():
print 'Jy is in Class A'

class B:

def druk():
print 'Jy is in Class B'

a = A()
c = B()
c.druk
a.druk
---------------------------------------------


--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies@futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------

"One thing have I desired of the LORD, that will I seek
after; that I may dwell in the house of the LORD all
the days of my life, to behold the beauty of the LORD,
and to inquire in his temple."
Psalms 27:4
Please help me to understand classes [ In reply to ]
* Johann Spies
|
| Can somebody explain to me why the following program produces no
| output:

A good trick to learn in Python is that if you use the interpreter you
can fool around and figure things out much more easily and quicker
than if you use something that resembles the edit-compile-run loop of
Pascal.

| [...skipping class defs]
|
| a = A()
| c = B()
| c.druk
| a.druk

If you do this in the interpreter you get:

>>> a = A()
>>> c = B()
>>> c.druk
<method B.druk of B instance at 80bdd50>
>>> a.druk
<method A.druk of A instance at 809a4d8>

This is a general rule in Python: if you mention a function or method
by name you just get a reference to it. If you add parentheses it is
called.

>>> c.druk()
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: no arguments expected
>>> a.druk()
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: no arguments expected

In your example this failed, since you forgot the self argument. If
you redeclare as:

class A:

def druk(self):
print 'Jy is in Class A'

To illustrate:

>>> a=A()
>>> a.druk
<method A.druk of A instance at 80b1e60>
>>> a.druk()
Jy is in Class A
>>> method=a.druk
>>> method
<method A.druk of A instance at 80b1e60>
>>> method()
Jy is in Class A


--Lars M.
Please help me to understand classes [ In reply to ]
From: Lars Marius Garshol <larsga@ifi.uio.no>


* Johann Spies
|
| Can somebody explain to me why the following program produces no
| output:

A good trick to learn in Python is that if you use the interpreter you
can fool around and figure things out much more easily and quicker
than if you use something that resembles the edit-compile-run loop of
Pascal.

| [...skipping class defs]
|
| a = A()
| c = B()
| c.druk
| a.druk

If you do this in the interpreter you get:

>>> a = A()
>>> c = B()
>>> c.druk
<method B.druk of B instance at 80bdd50>
>>> a.druk
<method A.druk of A instance at 809a4d8>

This is a general rule in Python: if you mention a function or method
by name you just get a reference to it. If you add parentheses it is
called.

>>> c.druk()
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: no arguments expected
>>> a.druk()
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: no arguments expected

In your example this failed, since you forgot the self argument. If
you redeclare as:

class A:

def druk(self):
print 'Jy is in Class A'

To illustrate:

>>> a=A()
>>> a.druk
<method A.druk of A instance at 80b1e60>
>>> a.druk()
Jy is in Class A
>>> method=a.druk
>>> method
<method A.druk of A instance at 80b1e60>
>>> method()
Jy is in Class A


--Lars M.
Please help me to understand classes [ In reply to ]
On Tue, 29 Jun 1999, Stidolph, David wrote:

> Finally, a simple question I can answer!
>
> First of all, class methods like "druk" need to have a parameter declared in
> them called self (or whatever you want to call it). Secondly you need to
> call the method with open/close parens:
>
> class A:
> def druk(self):
> print 'Jy is in Class A'
>
> class B:
> def druk(self):
> print 'Jy is in Class B'
>
> a = A()
> c = B()
> c.druk()
> Jy is in Class B
> a.druk()
> Jy is in Class A
>
> I hope this helps,

Thanks David. It helped. After your message I went back and read the
tutorial again and saw what you mean.

Johann



--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies@futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------

"Because he himself suffered when he was tempted, he
is able to help those who are being tempted."
Hebrews 2:18
Please help me to understand classes [ In reply to ]
From: Johann Spies <jhspies@futurenet.co.za>

On Tue, 29 Jun 1999, Stidolph, David wrote:

> Finally, a simple question I can answer!
>
> First of all, class methods like "druk" need to have a parameter declared in
> them called self (or whatever you want to call it). Secondly you need to
> call the method with open/close parens:
>
> class A:
> def druk(self):
> print 'Jy is in Class A'
>
> class B:
> def druk(self):
> print 'Jy is in Class B'
>
> a = A()
> c = B()
> c.druk()
> Jy is in Class B
> a.druk()
> Jy is in Class A
>
> I hope this helps,

Thanks David. It helped. After your message I went back and read the
tutorial again and saw what you mean.

Johann



--------------------------------------------------------------------------
| Johann Spies Windsorlaan 19 |
| jhspies@futurenet.co.za 3201 Pietermaritzburg |
| Tel/Faks Nr. +27 331-46-1310 Suid-Afrika (South Africa) |
--------------------------------------------------------------------------

"Because he himself suffered when he was tempted, he
is able to help those who are being tempted."
Hebrews 2:18