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


Groups > comp.lang.python > #97824

Re: variable scope of class objects

From Terry Reedy <tjreedy@udel.edu>
Subject Re: variable scope of class objects
Date 2015-10-19 20:03 -0400
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> <00fbd018-4188-40da-bacf-394d762c9bed@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.45.1445299445.878.python-list@python.org> (permalink)

Show all headers | 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