[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Attributes in privates methods

Yasser Almeida Hernández

2/19/2010 3:08:00 PM

Hi all.

I have a class with the attribute 'log_file', opened out of the class:
class ProteinCluster:
def __init__(self,cluster_name,log_file):
...
self.log_file = log_file
...
Then i have a private method which write in the log_file:
def _read_structure(self, pdb_code):
...
...
self.log_file.write('blablabla')
...
...
When i run the script, it raises the error:
AttributeError: ProteinCluster instance has no attribute 'log_file'

My question is, the class attributes are valids in private methods,
like in publics methods?

Thanks


--
Yasser Almeida Hernández, BSc
Center of Molecular Inmunology (CIM)
Nanobiology Group
P.O.Box 16040, Havana, Cuba
Phone: (537) 214-3178
almeida@cim.sld.cu

----------------------------------------------------------------
Correo FENHI



2 Answers

Diez B. Roggisch

2/19/2010 3:22:00 PM

0

Am 19.02.10 16:08, schrieb Yasser Almeida Hernández:
> Hi all.
>
> I have a class with the attribute 'log_file', opened out of the class:
> class ProteinCluster:
> def __init__(self,cluster_name,log_file):
> ...
> self.log_file = log_file
> ...
> Then i have a private method which write in the log_file:
> def _read_structure(self, pdb_code):
> ...
> ...
> self.log_file.write('blablabla')
> ...
> ...
> When i run the script, it raises the error:
> AttributeError: ProteinCluster instance has no attribute 'log_file'
>
> My question is, the class attributes are valids in private methods, like
> in publics methods?

There is no semantical difference there, you must have some other error.

Diez

Bruno Desthuilliers

2/19/2010 4:27:00 PM

0

Yasser Almeida Hernández a écrit :
> Hi all.
>
> I have a class with the attribute 'log_file', opened out of the class:
> class ProteinCluster:
> def __init__(self,cluster_name,log_file):
> ...
> self.log_file = log_file
> ...
> Then i have a private method which write in the log_file:
> def _read_structure(self, pdb_code):
> ...
> ...
> self.log_file.write('blablabla')
> ...
> ...
> When i run the script, it raises the error:
> AttributeError: ProteinCluster instance has no attribute 'log_file'
>
> My question is, the class attributes are valids in private methods, like
> in publics methods?

Diez already answered - the above code seems correct, so the source of
your problem is elsewhere.

For the record, in your code, log_file is an instance attribute - a
'class attribute' is an attribute that is defined on the class object
itself, and is shared by all instances of the class.

Also, Python has no notion of "public" or "private" - at least at the
language level. The '_name' convention is just, well, a naming convention.

This wont solve your problem - sorry, not much we can do here - but at
least it will help wrt/ mutual understanding !-)