3 Replies - 2179 Views - Last Post: 15 September 2010 - 07:04 AM Rate Topic: -----

#1 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Are all class members by default public

Posted 14 September 2010 - 10:24 PM

Hello


I am trying to find out if a python class can have private & public member variables or are all class variables(self) variables public?

Is there a way to make a class variable private in python?

for example; if I have this class, all the variables within that class(the self. ones) are public, correct? Is there a way to make them private?

class MyClass:


    def __init__( self, id ):

        self.id = id
        self.name = "blah" 
        
    def set_name( self, new_name ):

        self.name = new_name

    def get_name():

        return self.name


class = MyClass( 1, "hj" )

name = class.get_name() # I can get name this way
bad_way_name = class.name # I dont want to be able to get the name this way
 




Is This A Good Question/Topic? 0
  • +

Replies To: Are all class members by default public

#2 Guest_c.user*


Reputation:

Re: Are all class members by default public

Posted 15 September 2010 - 12:10 AM

>>> class A():
...     __i = 1
...     j = 2
...     def __f1(self):
...         print("f1")
...     def f2(self):
...         print("f2")
...     def f3(self):
...         print(self.__i)
...         self.__f1()
... 
>>> obj = A()
>>> obj.__i
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute '__i'
>>> obj.__f1()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute '__f1'
>>> obj.f2()
f2
>>> obj.f3()
1
f1
>>> 



This post has been edited by c.user: 15 September 2010 - 12:12 AM

Was This Post Helpful? 1

#3 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Are all class members by default public

Posted 15 September 2010 - 05:44 AM

Python doesnt really have private attributes. You can start a variable name with one or two underscores.

Starting with one underscore is only a naming convention. It doesnt make the variable private at all. It only tells people reading your code: "This variable is considered private" But it is still accessible from the outside.

Starting with two underscores uses "name mangling" on the variable name. If the name was __i in class A, that name will be changed to _A__i before any code is executed. Its main use is to avoid nameclashes with a subclass. But again it is accessible from the outside, though by a different name:

>>> class A:
...     __i = 1
... 
>>> a = A()
>>> a.__i
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    a.__i
AttributeError: A instance has no attribute '__i'
>>> a._A__i
1


This post has been edited by Nallo: 15 September 2010 - 05:46 AM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Are all class members by default public

Posted 15 September 2010 - 07:04 AM

Strangely, trailing underscores bugger up the name mangling:
>>> class Foo:
...     def a(self): print "Hi"
...     def __b(self): print "Bye"
...     def __c__(self): print "Huh?"
... 
>>> a = Foo()
>>> dir(a)
['_Foo__b', '__c__', '__doc__', '__module__', 'a']
>>> 



Not sure why.

As already stated, privacy contracts don't really exist in Python. Indeed, you can think of Python programming as an exercise in convention. No types are ever explicitly stated. I pass you an object and you assume it has properties and methods you want to use. It doesn't really matter the type, as long as the attributes I expect exist, we doing fine in Python.

The loose typed or duct typed languages like Python used to bug me a lot. As with any language, it's just how design decision make you lokk at a problem. Fail on compile or runtime, you decide. As languages go, Python is one of my favorites to play in, for some of the very reasons that used to annoy me. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1