basic class question..

P

Pyrot

class rawDNA:
import string
trans = string.maketrans("GATC","CTAG")


def __init__(self, template = "GATTACA"):
self.template = template //shouldn't this make "template"
accessible within the scope of "rawDNA"??


def noncoding(self):
print template.translate(trans) //
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
test.noncoding()
File "<pyshell#25>", line 7, in noncoding
print template.translate(trans)
NameError: global name 'template' is not defined

I'm curious .. what am I doing wrong??

P.S

class rawDNA:
import string
trans = string.maketrans("GATC","CTAG")
def __init__(self, template = "GATTACA"):
self.template = template
def noncoding(self):
print self.template.translate(trans)

this works as intended.

Thanks in advance
 
D

Diez B. Roggisch

Pyrot said:
class rawDNA:
import string

Importing here is unusual. Unless you have good reasons to do so, I
suggest you put the imports on top of the file.
trans = string.maketrans("GATC","CTAG")


def __init__(self, template = "GATTACA"):
self.template = template //shouldn't this make "template"
accessible within the scope of "rawDNA"??
No.


def noncoding(self):
print template.translate(trans) //

This needs to be

print self.template.translate(trans)

Thes scopes insied a class are only the method-locals (to which the
parameters count of course), and globals.

Diez
 
T

Tim Chase

Pyrot said:
class rawDNA:
import string
trans = string.maketrans("GATC","CTAG")


def __init__(self, template = "GATTACA"):
self.template = template //shouldn't this make "template"
accessible within the scope of "rawDNA"??

No. Python's scope resolution consists only of "local, global,
or explicit". There is no "try to find this in the instance or
class" scope-resolution guessing. Your code makes "template"
accessible within the scope of "self", not in a global
unqualified scope.

So this:
def noncoding(self):
print template.translate(trans) //

tries to reference "template" first in the local scope (within
noncoding(), but it doesn't exist there), then in the global
scope (it also doesn't exist there), and stops.

It should be as you have it here:
class rawDNA:
import string
trans = string.maketrans("GATC","CTAG")
def __init__(self, template = "GATTACA"):
self.template = template
def noncoding(self):
print self.template.translate(trans)

Here, you start with "self" which *is* in the local scope, which
*does* contain "template" and so it successfully finds it and all
is [qualifiedly] good. However, you'll also find that "trans"
isn't found because it's neither in the local nor global scope:

... import string
... trans = string.maketrans("GATC", "CTAG")
... def __init__(self, template="GATTACA"):
... self.template = template
... def noncoding(self):
... print self.template.translate(trans)
... Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in noncoding
NameError: global name 'trans' is not defined

so you need to fully qualify it as "RawDNA.trans" for Python to
find it. (I also shifted to using the PEP-8 naming convention
"RawDNA" instead of "rawDNA").

Which you indeed discovered:
this works as intended.

Being explicit is part of "Python Zen" (from the python
command-prompt, type "import this" to see the whole list)

-tkc
 
D

Diez B. Roggisch

Diez said:
Importing here is unusual. Unless you have good reasons to do so, I
suggest you put the imports on top of the file.


This needs to be

print self.template.translate(trans)

Ah, sorry, that should have been

self.template.translate(self.trans)

Diez
 
P

Pyrot

Pyrot said:
class rawDNA:
import string
trans = string.maketrans("GATC","CTAG")
def __init__(self, template = "GATTACA"):
self.template = template //shouldn't this make "template"
accessible within the scope of "rawDNA"??

No. Python's scope resolution consists only of "local, global,
or explicit". There is no "try to find this in the instance or
class" scope-resolution guessing. Your code makes "template"
accessible within the scope of "self", not in a global
unqualified scope.

So this:
def noncoding(self):
print template.translate(trans) //

tries to reference "template" first in the local scope (within
noncoding(), but it doesn't exist there), then in the global
scope (it also doesn't exist there), and stops.

It should be as you have it here:
class rawDNA:
import string
trans = string.maketrans("GATC","CTAG")
def __init__(self, template = "GATTACA"):
self.template = template
def noncoding(self):
print self.template.translate(trans)

Here, you start with "self" which *is* in the local scope, which
*does* contain "template" and so it successfully finds it and all
is [qualifiedly] good. However, you'll also find that "trans"
isn't found because it's neither in the local nor global scope:
... import string
... trans = string.maketrans("GATC", "CTAG")
... def __init__(self, template="GATTACA"):
... self.template = template
... def noncoding(self):
... print self.template.translate(trans)
...Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in noncoding
NameError: global name 'trans' is not defined

so you need to fully qualify it as "RawDNA.trans" for Python to
find it. (I also shifted to using the PEP-8 naming convention
"RawDNA" instead of "rawDNA").

Which you indeed discovered:
this works as intended.

Being explicit is part of "Python Zen" (from the python
command-prompt, type "import this" to see the whole list)

-tkc

thanks!

one last question, is "self.template.translate(trans)" the right way
to go(pythonic?)?
I found it to be cumbersome(and a mouthful) and thought I might have
been doing it wrong :)
 
P

Pyrot

Importing here is unusual. Unless you have good reasons to do so, I
suggest you put the imports on top of the file.



This needs to be

   print self.template.translate(trans)

Thes scopes insied a class are only the method-locals (to which the
parameters count of course), and globals.

Diez

Thanks for the tip Diez.

(Tthe core reason that I'm bothering with this at all is because I
heard imports are costly(in time, space, processing power). If this
turns out to be a non-issue, than my questions regarding Imports are
all moot :->)

I forgot to write about my second question which was:

what happens when I use the import statement within a class/function
declaration?
I'm thinking either
1) It imports during the class/function declaration
2) It imports the first time a class/function call(x = rawDNA() )
occurs

But if it's 2) then is the import valid outside of the function/class?
what happens when the last function reference is removed?(del x)

obviously this is a lot of questions...

I respect your(or anyone who would like to help me) time, so all I ask
is some kind of document or "Best practices" guide dealing all about
"import".(because sadly, http://docs.python.org/reference/simple_stmts.html#the-import-statement
does not ask my questions)
 
R

r

what happens when I use the import statement within a class/function
declaration?
I'm thinking either
1) It imports during the class/function declaration
2) It imports the first time a class/function call(x = rawDNA() )
occurs

But if it's 2) then is the import valid outside of the function/class?
what happens when the last function reference is removed?(del x)


Well just fire up you interpretor fella!
def __init__(self):
import sys
print sys.version

a = A() 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
'sys' in dir() False
print sys.version

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
print sys.version
NameError: name 'sys' is not defined

;-)
 
S

Steven D'Aprano

[...]
(Tthe core reason that I'm bothering with this at all is because I heard
imports are costly(in time, space, processing power). If this turns out
to be a non-issue, than my questions regarding Imports are all moot :->)

Importing a module is only costly the first time (in any session) that
you do so. Modules are cached, so the second time it is relatively fast.

I forgot to write about my second question which was:

what happens when I use the import statement within a class/function
declaration?
I'm thinking either
1) It imports during the class/function declaration
2) It imports the first time a class/function call(x = rawDNA() ) occurs

It imports when the class/function statement is *run*.

def f():
import math

The body of the function doesn't run until you call the function, so the
import doesn't happen until you call f().

class K:
import math
def method(self):
import string

The class definition runs when you create the class, which imports math.
But string is only imported when you call K().method().
But if it's 2) then is the import valid outside of the function/class?

No. The *name* "math" exists only inside the scope of the function, or
class, and is not visible outside.
.... import math
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined


what happens when the last function reference is removed?(del x)

You wrote:

x = rawDNA()

Deleting x won't do anything to the class rawDNA, or to the module string
which is referenced by the rawDNA class.

If you delete the rawDNA class, that won't do anything to the string
module cached in sys.


I respect your(or anyone who would like to help me) time, so all I ask
is some kind of document or "Best practices" guide dealing all about
"import".


99.9% of the time you don't need to worry about it. Just import the
modules you want at the top level of your program, and be happy that it
all works fine.

The other 0.1% of the time, you might have one or two good reasons for
putting imports inside functions:

(1) Encapsulation: you want to import a module to use in a function,
without making it visible to other functions.

(2) Just In Time importing. Imports at the top level slow down your
program's start up. Normally, this is trivial and it is not worth the
extra complexity to do something about it, but if you have a particularly
expensive module which happens to be used only in one function which is
rarely needed, then you can speed up your program for the majority of
times by putting the import inside that one function.


However, putting imports inside functions which are then called by
threads can lead to deadlocks. So you also have to balance the risk of
that happening.
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top