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: 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 Subject: Re: variable scope of class objects Date: Mon, 19 Oct 2015 20:03:43 -0400 References: <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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