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


Groups > comp.lang.python > #97824

Re: variable scope of class objects

Path csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'posting.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'jan': 0.11; 'def': 0.13; 'argument': 0.15; 'interpreter': 0.15; 'variables': 0.15; 'given)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'wrote:': 0.16; 'variable': 0.18; 'affects': 0.22; 'parameter': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'itself,': 0.29; 'creating': 0.30; 'code': 0.30; 'class.': 0.30; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'accessible': 0.33; 'instances': 0.33; 'changing': 0.34; 'could': 0.35; 'instance': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'test': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'email addr:gmail.com': 0.62; 'examples.': 0.84; 'self.value': 0.84; 'received:fios.verizon.net': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: variable scope of class objects
Date Mon, 19 Oct 2015 20:03:43 -0400
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> <00fbd018-4188-40da-bacf-394d762c9bed@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-59-124-74.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
In-Reply-To <00fbd018-4188-40da-bacf-394d762c9bed@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.45.1445299445.878.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1445299445 news.xs4all.nl 23865 [2001:888:2000:d::a6]:38580
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97824

Show key headers only | View raw


On 10/19/2015 7:19 PM, sohcahtoa82@gmail.com wrote:

> Class variables are accessible without creating an instance of a class.  Also, changing the value of a class variable affects ALL instances of that class.  This is because the variable belongs to the class itself, not any of the instances of that class.
>
> "self" is used to tell the interpreter that the variable/function you are accessing is a member of an instance of that class.

Wrong.  The first parameter of a method is an instance of a class.  It 
is conventionally called 'self' but could be any other name.  I use 's' 
for quick private test examples.

> Here's an example:
>
> class MyObject(object):
>      count = 0
>      def __init__(value):

This should be def __init__(self, value):

>          MyObject.count += 1
>          self.value = value
>
>      def printStuff():

       def print(self):

>          print("My value is ", self.value)

 > a = MyObject('a')
In 2.7.10
TypeError: __init__() takes exactly 1 argument (2 given)
Please run code before posting.

-- 
Terry Jan Reedy

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


Thread

variable scope of class objects JonRob - 2015-10-19 14:39 -0400
  Re: variable scope of class objects Random832 <random832@fastmail.com> - 2015-10-19 15:01 -0400
    Re: variable scope of class objects JonRob - 2015-10-20 17:11 -0400
  Re: variable scope of class objects sohcahtoa82@gmail.com - 2015-10-19 16:19 -0700
    Re: variable scope of class objects Terry Reedy <tjreedy@udel.edu> - 2015-10-19 20:03 -0400
  Re: variable scope of class objects Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-20 07:31 +0200
  Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-20 08:17 +0200
    Re: variable scope of class objects Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-20 08:38 +0200
      Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-20 09:23 +0200
    Re: variable scope of class objects JonRob - 2015-10-20 17:33 -0400
      Re: variable scope of class objects Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-10-20 20:18 -0400
        Re: variable scope of class objects JonRob - 2015-10-21 19:35 -0400
          Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-22 11:59 +0200
      What does it mean for Python to have “constants”? (was: variable scope of class objects) Ben Finney <ben+python@benfinney.id.au> - 2015-10-21 11:27 +1100
      Re: What does it mean for Python to have “constants”? Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-21 08:13 +0200
      Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-22 07:55 +0200
      Re: variable scope of class objects Erik <python@lucidity.plus.com> - 2015-10-20 23:17 +0100

csiph-web