Newbie: Classes

M

Michael Loomington

This is taken from the tutorial:.... def __init__(self, realpart, imagpart):
.... self.r = realpart
.... self.i = imagpart

So this is a constructor correct because of the __init__ ?
What is the self mean exactly? And if I create another def can that def see
my r and i variables and can I call them?
So I can do something like?

def real():
return r

In java you declare your variables outside of methods that's why I'm a
little confused.

The self just means its a constructor? So really it has two parameters?

Also, I read somewhere you don't need to specify the parameters? So how can
you assign r and i to 0 if the user doesn't specify the parameters?

In java if you call the instance of a class it returns a string if you wrote
a toString() method in that class. How can I do that in python?

Thanks.
 
K

KefX

All methods have 'self' as the first parameter. The actual name is unimportant,
but out of convention, practically everybody who's anybody uses 'self'.

What it is is an instance of a class. In C++ and Java, the same concept is
expressed with the 'this' keyword; Python just makes it explicit in having it
passed into the function.

So if I do this:
foobar = MyClass()
foobar.baz(2)

It is the same as:
foobar = MyClass()
MyClass.baz(foobar, 2)

Get it?
- Kef
 
D

Dietrich Epp

This is taken from the tutorial:
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart

So this is a constructor correct because of the __init__ ?
What is the self mean exactly? And if I create another def can that
def see
my r and i variables and can I call them?

A good resource is the tutorial, good reading for starting out:
http://python.org/doc/current/tut/tut.html

Yes, the __init__ makes this a constructor. 'self' is the object you
are constructing, like 'this' in C (Java?).
So I can do something like?
def real():
return r

'r' isn't a variable... it's a part of self. Try:

def real(self):
return self.r

But this is a bad example, because if you want the value of 'r' you
could just access it directly (fields aren't generally private in
Python).
In java you declare your variables outside of methods that's why I'm a
little confused.

In Python you don't have to declare your variables or fields. In fact,
in Python, you don't have to declare anything.
The self just means its a constructor? So really it has two
parameters?

The 'self' is the object. There are no hidden parameters in Python.
This has its pros and cons, for example it lets you add another method
to a class (or instance) dynamically.
Also, I read somewhere you don't need to specify the parameters? So
how can
you assign r and i to 0 if the user doesn't specify the parameters?

You need to specify any parameter without a default. So, in...

__init__(self,r,i)

....you need to supply the r and i parameters (self is supplied
automatically), but in...

__init__(self,r=0,i=0)

....you can call Complex(), Complex(1), Complex(i=0.74), ad nauseam.
In java if you call the instance of a class it returns a string if you
wrote
a toString() method in that class. How can I do that in python?

Use __str__...

def __str__(self):
return str(self.r) + ' + ' + str(self.i) 'j'
 
J

Jason Kratz

KefX said:
All methods have 'self' as the first parameter. The actual name is unimportant,
but out of convention, practically everybody who's anybody uses 'self'.

What it is is an instance of a class. In C++ and Java, the same concept is
expressed with the 'this' keyword; Python just makes it explicit in having it
passed into the function.

If I remember correctly Java also passes 'this' into funtions but its
done implicitly. Its there but you just don't see it.

Jason
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Jason Kratz
(e-mail address removed)
http://jkratz.dyndns.org/~jason/blog
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
K

KefX

If I remember correctly Java also passes 'this' into funtions but its
done implicitly. Its there but you just don't see it.

Exactly, but it may or may not do it exactly the same way it does parameters;
in Java it's an implementation detail, whereas in Python it's part of all of
the methods' interfaces. So that's why I didn't say it gets "passed in", but in
a sense, it is.

It should be noted that ALL of the methods of a class should have the 'self'
parameter, or else you can't do instance.method(), only classname.method(), and
I don't think you really gain from this arbitrary restriction.

- Kef
 
M

Michael Loomington

I think i'm getting the hang of it. But if I want to implement
multiplication? i.e. If c is an instance of Complex, then c*2 would multiply
r*2 and i*2 and return a new class that has the number multiplied by 2..... def __init__(self, realpart, imagpart):
.... self.r = realpart
.... self.i = imagpart

I tried doing the following but it doesn't work:

def __mul__(self, m)
self.r= self.r * m
self.i= self.i * m
return Complex(self.r, self.i)
 
M

Michael Loomington

I think i'm getting the hang of it. But if I want to implement
multiplication? i.e. If c is an instance of Complex, then c*2 would multiply
r*2 and i*2 and return a new class that has the number multiplied by 2.
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart

I tried doing the following but it doesn't work:

def __mul__(self, m)
self.r= self.r * m
self.i= self.i * m
return Complex(self.r, self.i)
Wait, I just create new variables and assign them to self.r*m and it works.
 
S

Sean Ross

Michael Loomington said:
I tried doing the following but it doesn't work:

def __mul__(self, m)
self.r= self.r * m
self.i= self.i * m
return Complex(self.r, self.i)

Hi. Python allows you to define behaviour for both left and right
multiplication (instance*2 or 2*instance). See if this helps:

def __mul__(self, m):
self.r *= m
self.i *= m
return Complex(self.r, self.i)

def __rmul__(self, m):
return self.__mul__(m)

Sean
 
A

Andrew Bennetts

Hi. Python allows you to define behaviour for both left and right
multiplication (instance*2 or 2*instance). See if this helps:

def __mul__(self, m):
self.r *= m
self.i *= m
return Complex(self.r, self.i)

Uh, I don't think you want to mutate this instance...

-Andrew.
 
K

KefX

def __mul__(self, m):
Uh, I don't think you want to mutate this instance...

Yeah, that's right. Do it like this instead:
def __mul__(self, m):
r *= m
i *= m
return Complex(r, m)

Why are you doing a complex class, anyway? Python already supports complex
numbers.

- Kef
 
K

KefX

Yeah, that's right. Do it like this instead:
def __mul__(self, m):
r *= m
i *= m
return Complex(r, m)

err...I meant this for the last line:
return Complex(r, i)

- Kef
 
B

Bjorn Pettersen

(e-mail address removed) (KefX) wrote in
err...I meant this for the last line:
return Complex(r, i)

it's late, but I think you meant

def __mul__(self, m):
r = self.r * m
i = self.i * m
return Complex(r, i)

-- bjorn
 
R

ryan scott

Michael said:
This is taken from the tutorial:

... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart

So this is a constructor correct because of the __init__ ?

Yes, this is the constructor for the Complex class

What is the self mean exactly? And if I create another def can that def see
my r and i variables and can I call them?
So I can do something like?

def real():
return r

In java you declare your variables outside of methods that's why I'm a
little confused.

The self refers to the specific instance of that class that you create.
So when you call a method on an instance, the self parameter represents
the instance doing the calling.

For example,when you create the following objects
complex1 = Complex(3.0, -4.5)

complex2 = Complex(3.5, -5.6)

and then call
complex1.real(), it will return 3.0 and complex2.real() will return
3.5.

To use instance variables in your methods you have to append the
variable with self and have self as a parameter. So your real method
would become

def real(self):
return self.r



The self just means its a constructor? So really it has two parameters?
Yes


Also, I read somewhere you don't need to specify the parameters? So how can
you assign r and i to 0 if the user doesn't specify the parameters?
In java if you call the instance of a class it returns a string if you wrote
a toString() method in that class. How can I do that in python?

You can override the __str__ method.

def __str__(self):
print "toString"

Ryan
 

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

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top