On Thursday, March 10, 2016 at 1:36:48 PM UTC-6, Mark Lawrence wrote: > On 10/03/2016 14:57, Dan Strohl via Python-list wrote: > >> I've been studying Object Oriented Theory using Java. Theoretically, all > >> attributes should be private, meaning no one except the methods itself can > >> access the attribute; > >> > >> public class Foo { > >> private int bar; > >> ... > > > > For the benefit of any newbies/lurkers I'll just point out > that this might well be valid Java, but... > > > class person: > > age = 21 > > name = 'Cool Dude' > > > ...this gives you class attributes, so the age is always > 21 and the name is always 'Cool Dude'. So you can vary > the age and name you'd need:- > > class person(): > def __init__(self, age, name): > self.age = age > self.name = name
Mark, you've made some absurd claims in the past, but are you *REALLY* expecting this group of highly intelligent people to believe, that class attributes are read-only? ############################################################ # BEGIN INTERACTIVE SESSION ############################################################ PY> class Foo(object): ... age=21 ... name = 'cool dude' ... PY> Foo.name cool dude PY> Foo.age 21 PY> Foo.age = 25 PY> Foo.age 25 ############################################################ # END INTERACTIVE SESSION ############################################################ And furthermore, do you realize that the "new code" you posted, creates instance level variables, *NOT* class level variables? ############################################################ # BEGIN INTERACTIVE SESSION ############################################################ PY> class Foo(object): ... def __init__(self, name, age): ... self.name = name ... self.age = age ... PY> Foo.name AttributeError: type object 'Foo' has no attribute 'name' PY> Foo.age AttributeError: type object 'Foo' has no attribute 'age' ############################################################ # END INTERACTIVE SESSION ############################################################ Of course, "name" and "age" would be more appropriate when defined as instance variables, since both are attributes specific to each person. A class level variable should only be used for an attribute that is shared by *ALL* instances of the class, and one that is *NOT* specific to any one of them. For instance, something highly general like keeping a tally of the total number of "Persons" created, or the total number of persons with red hair, or with green eyes. These are the more generalized attributes of *ALL* persons, that are not specific to any *ONE* person. By offering example code that attempts to initialize class level variables in the constructor, you have exposed yourself as not understanding what a class level variable *IS*. You *DON'T* initialize class level variables, no, you assign them to the class object. Class level variables are intended to be used as "shared state" between multiple instances, not "initialized" by each instance. But don't feel bad. Most folks would be surprised at the high percentage of "seasoned programmers" who do not understand the difference between class level, and instance level variables. I've have personally witnessed someone say aloud: "Class variables are useless, always use instance variables instead". Oh my! o_O There are two types of variables you can create relative to a class: instance level variables, and class level variables. In python, both of them can be read and written at will, however, creating them and accessing them is slightly different. PS: Ignorance of the unknown produces fear, and fear can produce unwarranted hatred. I have suspected for many years that those in the Python community who are constantly bad- mouthing OOP, are displaying an emotional reaction rooted in the psychological distress of ignorance and fear, and your posting here has provided evidence of that distress. -- https://mail.python.org/mailman/listinfo/python-list