Thread: Class problem

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

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Class problem


    So im struggling with classes in my C# course, so i decided to do it in python first because the syntax is generaly easier.
    So i took this lil code snippit from the book, but i keep getting an attribute error and i am not sure why.
    ----------------------------------------------------
    import random

    class coin:

    # This defines the sideup attribute
    def coin(self):
    self.sideup = 'heads'

    # This is the function that determines heads or tails
    def toss(self):
    if random.randint(0, 1) == 0:
    self.sideup = 'heads'
    else:
    self.sideup = 'tails'

    # line 29 is where i am getting my attribute error
    def get_sideup(self):
    return self.sideup

    def main():
    myCoin = coin()

    print ("test123", myCoin.get_sideup())

    print ("test1234", myCoin.toss())

    print ("test12345", myCoin.get_sideup())
    main()
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    First use code tags when you post a test case, which makes the code readable for those who read your comment.

    It's not really clear what you want to do, but if you want to initialize/create some instance attribute upon creating the instance, you have to create the constructor method __init__

    For more information please read the following
    http://docs.python.org/release/3.2.3/tutorial/classes.html#a-first-look-at-classes
    ... The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__() ...
    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2
    on line 6 where it says : def coin(self):
    you would want it to say def __init__(self)

    the coin method inside the coin class is not run automatically.
    so self.sideup never gets defined.

    try to put your code into code brackest next time you post anything, so ppl dont have to figure out the indentation by themselves.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    @devdoc345

    And here is an example of using constructors using Python 3
    Code:
    class Student:
    
        def __init__(
                        self,
                        param_studentID, 
                        param_firstName, 
                        param_lastName
                     ):
            self.__studentID = param_studentID
            self.__firstName = param_firstName
            self.__lastName = param_lastName
            
        def getStudentID(self):
            return self.__studentID
    
            
    def main():
        student = Student("id-000001", "Harry", "Potter")
        print (student.getStudentID())
        
    main()

    Regards,
    Dariyoosh
    Last edited by dariyoosh; March 3rd, 2013 at 04:51 AM.

IMN logo majestic logo threadwatch logo seochat tools logo