When *don't* I use 'self' in classes?

A

Adam Gaskins

I am a bit confused as too when, if ever, it is not appropriate to prepend
'self' to objects in a class. All of the examples of how to use 'self' that
I find seem to be short and very simple (as examples tent to be). I
appologize if I am asking an ignorant question here, but I want to get off
on the right foot. Here's an example of what I mean:

import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
self.ser = serial
try:
self.ser.Serial()
self.ser.baudrate = self.baudrate
self.ser.open()
except:
print 'Serial port could not be opened'

=== OR ===
import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
try:
ser = serial.Serial()
ser.baudrate = self.baudrate
ser.open()
except:
print 'Serial port could not be opened'

There may be a typo in here, this is just a random example similar to
something I'm working with, but which one of these are more 'proper'? If I
am importing a library do I still prepend it's object with self when I use
it in my class? I suppose my question is just basically... when do you NOT
prepent an object in a class with 'self'?

I'm not even sure I'm using the term 'object' correctly here. Feel free to
set me straight, but I hope my example makes it clear what I am asking.

Thanks a lot, this ng has already been super helpful as I take my
crash-course in to python! :p
 
R

Rhodri James

I am a bit confused as too when, if ever, it is not appropriate to
prepend
'self' to objects in a class. All of the examples of how to use 'self'
that
I find seem to be short and very simple (as examples tent to be). I
appologize if I am asking an ignorant question here, but I want to get
off
on the right foot. Here's an example of what I mean:

import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
self.ser = serial
try:
self.ser.Serial()
self.ser.baudrate = self.baudrate
self.ser.open()
except:
print 'Serial port could not be opened'

=== OR ===
import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
try:
ser = serial.Serial()
ser.baudrate = self.baudrate
ser.open()
except:
print 'Serial port could not be opened'

There may be a typo in here, this is just a random example similar to
something I'm working with, but which one of these are more 'proper'? If
I am importing a library do I still prepend it's object with self when I
use it in my class? I suppose my question is just basically... when do
you NOT prepent an object in a class with 'self'?

I'm not even sure I'm using the term 'object' correctly here. Feel free
to set me straight, but I hope my example makes it clear what I am
asking.

Prepending "self" to an identifier means that what you are using is an
attribute of the class instance that you're dealing with; i.e. you're
attaching the object to the instance. Whether or not you want to do that
depends entirely on whether you expect to use that object ever again, or
whether you're using it once and tossing it away.

For the example you give, you're most likely to want to do neither of
the above. The first version gives you access to the entire namespace
of the "serial" module as an attribute of instances of the class "foo",
which is unlikely to be what you want. The second, on the other hand,
creates and opens a serial port, then throws it away. It's more likely
that you'll want something like this:

import serial
class foo:
def __init__(self, comport):
self.comport = comport
self.baudrate = 9600
try:
self.ser = serial.Serial()
self.ser.baudrate = self.baudrate
self.ser.open()
except:
print 'Serial port could not be opened'

def send_data(self, data):
self.ser.write(data)

(except that a bare 'except' like that is a very bad idea (you'll catch
keyboard interrupts and the like), but let's assume that you're doing
something more detailed for the purposes of this example.)

As you can see, making the serial port itself an attribute allows you
to use it in other methods. If you had just used "ser" as in your
second example, it would be a local variable to the method foo.__init__(),
so it would vanish when __init__ returned just like any other local
variable. Making it an attribute keeps it around (attached to just
that one instance of the class) so that you can use it later.

In general, don't make something an attribute if you know you're never
going to reuse it.
 
M

MRAB

Adam said:
I am a bit confused as too when, if ever, it is not appropriate to prepend
'self' to objects in a class. All of the examples of how to use 'self' that
I find seem to be short and very simple (as examples tent to be). I
appologize if I am asking an ignorant question here, but I want to get off
on the right foot. Here's an example of what I mean:

import serial
class foo:
def __init(self, comport):
^^^^^^
Should be '__init__'.
self.comport = comport
self.baudrate = 9600 #default
self.ser = serial
try:
self.ser.Serial()
self.ser.baudrate = self.baudrate
self.ser.open()
except:
print 'Serial port could not be opened'

=== OR ===
import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
try:
ser = serial.Serial()
ser.baudrate = self.baudrate
ser.open()
except:
print 'Serial port could not be opened'

There may be a typo in here, this is just a random example similar to
something I'm working with, but which one of these are more 'proper'? If I
am importing a library do I still prepend it's object with self when I use
it in my class? I suppose my question is just basically... when do you NOT
prepent an object in a class with 'self'?

I'm not even sure I'm using the term 'object' correctly here. Feel free to
set me straight, but I hope my example makes it clear what I am asking.

Thanks a lot, this ng has already been super helpful as I take my
crash-course in to python! :p
In the second example 'ser' is just a local variable within '__init__'.
 
T

Tim Chase

Adam said:
I am a bit confused as too when, if ever, it is not appropriate to prepend
'self' to objects in a class. All of the examples of how to use 'self' that
I find seem to be short and very simple (as examples tent to be). I
appologize if I am asking an ignorant question here, but I want to get off
on the right foot. Here's an example of what I mean:

import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
self.ser = serial
try:
self.ser.Serial()
self.ser.baudrate = self.baudrate
self.ser.open()
except:
print 'Serial port could not be opened'

=== OR ===
import serial
class foo:
def __init(self, comport):
self.comport = comport
self.baudrate = 9600 #default
try:
ser = serial.Serial()
ser.baudrate = self.baudrate
ser.open()
except:
print 'Serial port could not be opened'

There may be a typo in here,

Like "__init" instead of "__init__"? :)
am importing a library do I still prepend it's object with self when I use
it in my class? I suppose my question is just basically... when do you NOT
prepent an object in a class with 'self'?

Use self.<attribute> when you want the resulting "ser" object to
live beyond the __init__ call. Easily seen in this example:

class Foo:
def __init__(self):
self.abc = 42
xyz = 3.141
# xyz now falls out of scope

def test_me(self):
print self.abc # succeeds and prints 42
print xyz # fails because "xyz" doesn't exist
# within this scope

f = Foo()
print dir(f) # has an "abc" but not an "xyz"
f.test_me()

So in your case, unless you *need* to keep the comport/baudrate
around, I'd just use
-tim

DEFAULT_BAUD = 9600
class Foo:
def __init__(self, comport):
self.comport = comport
try:
self.ser = Serial()
self.ser.baudrate = DEFAULT_BAUD
self.ser.open()
except SomeSpecificException:
print "Fail!"

so that the .ser object is available in other method-calls.

-tkc
 
C

CM

I am a bit confused as too when, if ever, it is not appropriate to prepend
'self' to objects in a class. All of the examples of how to use 'self' that
I find seem to be short and very simple (as examples tent to be). I
appologize if I am asking an ignorant question here, but I want to get off
on the right foot. Here's an example of what I mean:

import serial
class foo:
    def __init(self, comport):
        self.comport = comport
        self.baudrate = 9600 #default
        self.ser = serial
        try:
            self.ser.Serial()
            self.ser.baudrate = self.baudrate
            self.ser.open()
        except:
            print 'Serial port could not be opened'

=== OR ===
import serial
class foo:
    def __init(self, comport):
        self.comport = comport
        self.baudrate = 9600 #default
        try:
            ser = serial.Serial()
            ser.baudrate = self.baudrate
            ser.open()
        except:
            print 'Serial port could not be opened'

There may be a typo in here, this is just a random example similar to
something I'm working with, but which one of these are more 'proper'? If I
am importing a library do I still prepend it's object with self when I use
it in my class? I suppose my question is just basically... when do you NOT
prepent an object in a class with 'self'?

I'm not even sure I'm using the term 'object' correctly here. Feel free to
set me straight, but I hope my example makes it clear what I am asking.

Thanks a lot, this ng has already been super helpful as I take my
crash-course in to python! :p

Others have said something like "use self prepended to an object if
you are
going to use it again". I'd prefer to think of it more like "use self
prepended to an object if you'd like other functions to be able to
know
about that object.". Is say this because you can have functions
called
over and over, and their contents "used again", but you still might
not
mind if their objects are local only to that function.

I just think of it that if I am going to need this object to be
generally
available to the whole class, I throw a self. on. If not, I might not
bother.
 
B

Bruno Desthuilliers

Tim Chase a écrit :
(snip)

try:
self.ser = Serial()
self.ser.baudrate = DEFAULT_BAUD
self.ser.open()
except SomeSpecificException:
print "Fail!"

Please make it:


try:
self.ser = Serial()
self.ser.baudrate = DEFAULT_BAUD
self.ser.open()
except SomeSpecificException, e:
print "Fail! reason : %s" % e
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top