0

main class:

class circle:

    def __init__(self,radius=1):

        self.radius=radius

    def getArea(self):
        return(3.142*self.radius*self.radius)
    def getPerimeter(self):
        return(2*3.142*self.myradius)
    def setradius(self,radius):
        if radius>0:
            self.__radius=radius
    def getRadius(self):
        return self.__radius

Using class:

from Circle import circle

def main():


    c1=circle()
    c1.radius=-1
    c2=circle(5)
    c3=circle(3)
    print(c1.getArea())
    print(c2.getArea())
    print(c3.getArea())

main()

Hello people!

I was just trying to learn class basics but was haven some trouble. Hope you guys can help. Thanks in advance :)

  1. What exactly is private variable? I read about it and people say that it cannot be accessed outside of class. OK! BUT WHAT DOES THAT MEAN EXACTLY?
  2. Is private data same as DATA HIDING?
  3. What exactly is self? I read about that too but could not understand from previous python answers. From what I think self is written after every def statement in class.

Thanks again! Cheers!

4
  • 1
    Your indentation in the circle class is currently not valid/ambiguous -- could you edit your post to fix it? Commented Apr 20, 2014 at 1:12
  • @Michael0x2a Hi is it better now? Commented Apr 20, 2014 at 1:15
  • No, the problem is that def __init__() needs to be on the same indentation level as the other def lines. Commented Apr 20, 2014 at 1:16
  • @Leigh It started to work. Thank you! Could you also possibly give some idea about the first 3? Commented Apr 20, 2014 at 1:19

1 Answer 1

2

Private Variables/Data Hiding/Name Mangling

Python does not have private instance variables that can be accessed only from within an object. You cannot enforce data hiding in Python. By convention, Python code uses a single underscore to indicate a name that is not considered public, but the name is still accessible outside of its object. You, however, are using name mangling (with two leading underscores). Name mangling replaces an identifier in the following manner:

self.__radius becomes self._classname__radius if classname is the name of the class.

Even mangled names, however, are accessible outside their class. The mangled name just reduces the likelihood of name collisions, since the class name becomes part of the attribute name.

self

self is by convention the name we use for an instance. So consider this class:

class Circle(object):
    def get_area(self):
        pass

The class is Circle, and we could assign an instance of Circle to a variable like this:

c = Circle()  # an instance

Now if we call the get_area method, we could do so like this:

c.get_area()  # note the lack of arguments

This is equivalent to the following:

Circle.get_area(c)  # instance c is now passed in as the first argument of `get_area`, which our method calls self

self, then, is an instance passed to a method as the first argument.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.