#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Private and public attributes


    Hi guys, I have a homework question but I dnt know where to start. Can someone help me:

    Books Inc. is a small bookshop that sells new and second hand
    books to the public. You are the programmer in this company.

    Study the incomplete coding below that will be used to manage the book
    The Book class represents a book. The data that should
    be kept as attributes in the class are as follows:
    the name of the author
    the title of the book
    the number of copies available

    The method for this class: init method will accept arguments
    for the author, title and number of copies available. Only the title
    should be made public.

    The display and sell book methods accept arguments and return
    values to the calling program.

    Complete the coding for (i), (ii) and (iii) using the above information.

    class Book(object):
    def __init__(self,author,title,copies):
    #The __init__ method must accept arguments for
    #the author (private attribute)
    #the title (public attribute) and
    #the number of copies (private atrribute)
    (i)
    def display(self):
    #The display method must display the information
    #for the book, that is the author, title and number of copies
    #available
    (ii)-

    def sell_book(self,sold):
    #This method must extract from the number of copies available
    #the number of copies sold.
    #Test that the user doesn't sell more copies than there are
    #available.
    #Display a suitable message if it was the last copy sold

    -(iii)

    def main():
    #create an instance of the Book class
    myBook = Book(author="JB Wellington", title="The digital divide",
    copies=40)
    myBook.sell_book(sold=3)
    myBook.display()
    #main
    main()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Originally Posted by b49P23TIvg
    This has been a source of confusion for me, because as the documentation (in the link you provided) indicates there is no such private mechanism in Python (unlike classic OO programming languages such as Java). Yet, when I prefix the attribute name by double underscores, I cannot access it anymore directly (as I can refer to a public attribute), so it seems to me that prefixing the name by double '_' provides some restriction (maybe I'm wrong). The following example was run with Python 3

    python Code:
    >>> class MyClass:
    ...     def __init__(self, p_size, p_id):
    ...         self.size = p_size
    ...         self.__id = p_id
    ...
    >>> obj = MyClass(p_size=28000, p_id="id-000001")
    >>> type(obj)
    <class '__main__.MyClass'>
    >>>
    >>> obj.size
    28000
    >>>
    >>> obj.__id
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'MyClass' object has no attribute '__id'

    Could you kindly make some clarification?
    Regards,
    Dariyoosh
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Honestly, I never understood or effectively used the name mangling either. As you say, python doesn't offer private data, and depends instead on the good will of programmers who want to write programs that work. Let's play with the underscores a little bit.
    Code:
    class c:
        _b = 'marks a subset of name space?'
        __a = 'becomes prefixed with _<class name>'
    
    class s(c):
        __a = 'should differ from _c__a'
    
    
    print(c._b)     # marks a subset of name space?
    print(c._c__a)  # becomes prefixed with _<class name>
    
    print(s._s__a)  # should differ from _c__a
    print(s._c__a)  # becomes prefixed with _<class name>
    To arrive at this final result involved a smattering of dir() and tested objects of these classes as well, c()._c__a for instance.
    Last edited by b49P23TIvg; August 27th, 2013 at 02:11 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo