------------------------------------------------------------------------
Temat:
Re: Changing the private variables content
Od:
Gary Herron <gher...@islandtraining.com>
Data:
Tue, 21 Jul 2009 14:14:44 -0700
Do:
Ryniek90 <rynie...@gmail.com>
Do:
Ryniek90 <rynie...@gmail.com>
Kopia:
python-list@python.org
Ryniek90 wrote:
Hi.
I'm writing some class, and decided to use inside private method and
some private variables. While with method i haven't got any problem's
with variables i have.
Maybe some example.
A class with private method and private variable:
"
>>> class Secret(object):
#
def __init__(self):
self._number = 1
#
def _secret(self):
print self._number
def showit(self):
print "Secret number is:\n"
self._secret()
>>> sec = Secret()
>>> sec.showit()
Secret number is:
1
>>> sec._number
1
>>> sec._number = 2
>>> sec._number
2
>>> sec._number += 3
>>> sec._number
5
>>>
"
As You can see, i made class with private method and private variable
inside __init__ constructor. I've changed also the variable value,
but outside class.
I've got problem with changing some variable value _inside__my_
class, which i'm writing.
Not sure this is what you are asking, but a method (which is how I
interpret "_inside__my_ class") changes the value by normal assignment
like this:
class Secret(object):
...
def SetNumber(self,value):
self._number = value
Is that what you were asking?
Gary Herron
I've searched over google, some tuts with topics about operations on
private variables, but didn't found anything - only how to make them,
but no how to assign new objects/override them with new content (and
other advanced and helpful options).
If someone could help me, it would be great.
Thanks.
Thanks for hint, but looks like i can't do what i want.
Maybe i show You my class:
"
class ModPrint(object):
u"""
This will be the doc.
"""
def __init__(self):
#Assign the Python installation directory - sys.exec_prefix - to
variable
self._default_search_path=sys.exec_prefix
self._chosen_module = ''
self._user_search_path = ''
self._this_module = ''
def _SetVar(self, attr, val):
self.attr = val
def _search_for_module(self, *args):
"""Private method which walks through default Python
installation directories, and search for prefered module."""
#walking thru all directories available in path '_user_search_path'
for root, dirs, files in os.walk(self._user_search_path):
for f in files:
#if found file 'f' is THAT module,
#full path to THAT file is assigned to variable
if f == ("%s.py" % self._chosen_module):
self._SetVar(self._this_module, os.path.join(root, f))
def print_module(self, _chosen_module='', _user_search_path='',
_default_search_path=sys.exec_prefix,):
"""Reads module chosen by user, and returns full content of this
module, as it is."""
#if custom search path hasn't been assigned,
#default path is being assigned as custom path
if self._user_search_path == '':
self._user_search_path = self._default_search_path
#using private method '_search_for_module' with 'self.' preffix
#to search for prefered module
self._search_for_module(_chosen_module, _user_search_path)
#opening prefered module with read-only binary mode
#and assigning it to 'module_open' variable
module_open = open(self._this_module, 'rb')
#reading this file and assigning it to variable
module_text = module_open.read()
#closing read file; the read content is still available
#it's stored in variable 'module_text'
module_open.close()
#returning the read content
return module_text
"
When i use this class in Python IDLE, i've got this error:
"
>>> mod = ModPrint()
>>> import socket
>>> mod.print_module('socket')
Traceback (most recent call last):
File "<pyshell#60>", line 1, in <module>
mod.print_module('socket')
File "<pyshell#57>", line 48, in print_module
module_open = open(self._this_module, 'rb')
IOError: [Errno 2] No such file or directory: ''
>>>
"
As You can see, i can't assign the new value "os.path.join(root, f)" to
the 'self._this_module variable'.
So for sure i've made some mistake in method:
"
def _SetVar(self, attr, val):
self.attr = val
"
When i've changed private variables to normal, stored in class (in
__init__ method), it was the same situation - i couldn't change this
variable value.
"
>>> mod = ModPrint()
>>> mod.print_module('os')
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
mod.print_module('os')
File "<pyshell#64>", line 48, in print_module
module_open = open(self.this_module, 'rb')
IOError: [Errno 2] No such file or directory: ''
>>>
"
Thanks i someone could help me, give some hint.
--
http://mail.python.org/mailman/listinfo/python-list