Attributes in privates methods

  • Thread starter Yasser Almeida Hernández
  • Start date
Y

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?

Thanks


--
Yasser Almeida Hernández, BSc
Center of Molecular Inmunology (CIM)
Nanobiology Group
P.O.Box 16040, Havana, Cuba
Phone: (537) 214-3178
(e-mail address removed)
 
D

Diez B. Roggisch

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
 
B

Bruno Desthuilliers

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 !-)
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top