Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10125

Re: Use self.vars in class.method(parameters, self.vars)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <karim.liateni@free.fr>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'python.': 0.04; 'instance': 0.05; 'mess': 0.07; 'python': 0.08; 'attribute': 0.09; 'attribute.': 0.09; 'namespace': 0.09; 'prefix': 0.09; 'referenced': 0.09; 'subject:parameters': 0.09; 'wrote:': 0.15; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'coupling': 0.16; 'from:addr:free.fr': 0.16; 'inherited': 0.16; 'linux2': 0.16; 'received:free.fr': 0.16; 'underscore': 0.16; 'pm,': 0.16; '>>>': 0.16; 'variable': 0.21; '(this': 0.22; 'modify': 0.22; 'header:In- Reply-To:1': 0.22; 'code.': 0.22; 'skip:k 20': 0.23; 'cheers': 0.23; 'says': 0.25; 'skip:[ 10': 0.26; 'convention': 0.29; 'fact': 0.30; 'class': 0.31; "skip:' 10": 0.32; 'apr': 0.32; 'it.': 0.33; 'external': 0.33; 'reference': 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'non': 0.36; 'trouble': 0.37; 'but': 0.37; 'using': 0.37; 'received:192': 0.38; 'subject:: ': 0.38; 'case': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'double': 0.62; 'huge': 0.64; 'discourage': 0.84
Date Fri, 22 Jul 2011 19:26:11 +0200
From Karim <karim.liateni@free.fr>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: Use self.vars in class.method(parameters, self.vars)
References <0ddc2626-7b99-46ee-9974-87439ae09f1e@e40g2000yqn.googlegroups.com> <4E295FBF.3040402@free.fr> <4E2967AF.7020501@jollybox.de> <mailman.1369.1311353663.1164.python-list@python.org> <4b8bf7a1-4d4f-4f72-9b5e-3dcce0dd6114@ft10g2000vbb.googlegroups.com>
In-Reply-To <4b8bf7a1-4d4f-4f72-9b5e-3dcce0dd6114@ft10g2000vbb.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1375.1311355578.1164.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311355578 news.xs4all.nl 23937 [2001:888:2000:d::a6]:32944
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10125

Show key headers only | View raw



Be careful when using double underscore prefix the variable is "said" to 
be private but in fact you can modify it. It is a convention to say 
don't change it. And to discourage to use it python change its name
to '_<class name>__myvar' appending the prefix '_<class name>' to it.

See with your example:

karim@Requiem4Dream:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> a = foo()
 >>> dir(a)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', '__weakref__', '_foo__myvar', 'foo2']

In the instance namespace your attribute was transformed in '_foo__myvar'.
This is a veritable mess when you want to access from outside your class 
this attribute.
For maintenance when you inherited you have huge coupling NEVER DO THAT 
(advice):
In case you change the name of your class and reference this attribute 
in external class
you will end up with huge trouble to change the name in all referenced code.
With one '_' it says to others well this is my non public variable don't 
use it (this is a convention
because you can still modify it in python.

Cheers
Karim



On 07/22/2011 06:59 PM, caccolangrifata wrote:
> while i<  len:

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Use self.vars in class.method(parameters, self.vars) caccolangrifata <caccolangrifata@gmail.com> - 2011-07-22 04:12 -0700
  Re: Use self.vars in class.method(parameters, self.vars) Karim <karim.liateni@free.fr> - 2011-07-22 13:32 +0200
  Re: Use self.vars in class.method(parameters, self.vars) Thomas Jollans <t@jollybox.de> - 2011-07-22 13:33 +0200
    Re: Use self.vars in class.method(parameters, self.vars) caccolangrifata <caccolangrifata@gmail.com> - 2011-07-22 05:02 -0700
  Re: Use self.vars in class.method(parameters, self.vars) Thomas Jollans <t@jollybox.de> - 2011-07-22 14:06 +0200
  Re: Use self.vars in class.method(parameters, self.vars) "bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com> - 2011-07-22 08:43 -0700
    Re: Use self.vars in class.method(parameters, self.vars) caccolangrifata <caccolangrifata@gmail.com> - 2011-07-22 09:50 -0700
      Re: Use self.vars in class.method(parameters, self.vars) Thomas Jollans <t@jollybox.de> - 2011-07-22 20:58 +0200
    Re: Use self.vars in class.method(parameters, self.vars) rantingrick <rantingrick@gmail.com> - 2011-07-22 11:38 -0700
      Re: Use self.vars in class.method(parameters, self.vars) Thomas Jollans <t@jollybox.de> - 2011-07-22 20:49 +0200
      Re: Use self.vars in class.method(parameters, self.vars) John Gordon <gordon@panix.com> - 2011-07-22 19:00 +0000
        Re: Use self.vars in class.method(parameters, self.vars) Chris Angelico <rosuav@gmail.com> - 2011-07-23 05:12 +1000
          Re: Use self.vars in class.method(parameters, self.vars) caccolangrifata <caccolangrifata@gmail.com> - 2011-07-22 12:41 -0700
        Re: Use self.vars in class.method(parameters, self.vars) rantingrick <rantingrick@gmail.com> - 2011-07-22 12:16 -0700
          Re: Use self.vars in class.method(parameters, self.vars) Chris Angelico <rosuav@gmail.com> - 2011-07-23 06:20 +1000
  Re: Use self.vars in class.method(parameters, self.vars) Karim <karim.liateni@free.fr> - 2011-07-22 18:54 +0200
    Re: Use self.vars in class.method(parameters, self.vars) caccolangrifata <caccolangrifata@gmail.com> - 2011-07-22 09:59 -0700
      Re: Use self.vars in class.method(parameters, self.vars) Karim <karim.liateni@free.fr> - 2011-07-22 19:26 +0200
  Re: Use self.vars in class.method(parameters, self.vars) Chris Torek <nospam@torek.net> - 2011-07-22 22:22 +0000

csiph-web