Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Michael Gichia
    @michaelgichia
    @clarch on your code look at line 20.
    age = age + 1
    clarch
    @clarch
    :smile: thanks @michaelgichia seems to work, I have a question though do I need to use self.initialAge again in yearPasses() if I already assigned it to age in my init, is there another way I can reference age outside of init
    Michael Gichia
    @michaelgichia
    Yes you do. init method is initialization of the object.
    Michael Gichia
    @michaelgichia
    Example: when a child is born we give this child a name. We refer to the child using the names he or she was given. For you to use or interact with the child(object) we must pass assign them the attribute name. As in your case, the attribute one needs to assign before interacting with person is initialAge.
    So you can refer to the attributes as long as you need it.
    You will refer to it as self.initialAge. It is globally available in class.
    clarch
    @clarch
    oh yea, makes sense, got it thanks :+1:
    Michael Gichia
    @michaelgichia
    anytime
    clarch
    @clarch
    oh wait the error disappears but it doesn't seem to increment the age though
    Michael Gichia
    @michaelgichia
    can I see your code?
    clarch
    @clarch
    actually it works when I use self.initialAge instead of assigning it to age like so
    class Person:
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
            self.initialAge = initialAge
            if (initialAge < 0):
                initialAge = 0
                print "Age is not valid, setting age to 0."
        def yearPasses(self):
            # Increment the age of the person in here        
            self.initialAge = self.initialAge + 1
    
    p = Person(10)  
    p.amIOld()
    for j in range(0, 3):
        p.yearPasses()        
    p.amIOld()
    print("")
    Michael Gichia
    @michaelgichia
    This message was deleted
    This message was deleted
    I'm not understanding p.amIOld() part.
    Michael Gichia
    @michaelgichia
    Give me few minutes I go through it.
    clarch
    @clarch
    oh sorry hehe that is another function that i was trying out
    i think for me to use 'age' as I was, it needs to be a global variable, wah these things usually confuse me, is it just me hehe?
    Michael Gichia
    @michaelgichia

    Condition should come before assignment.

            if (initialAge < 0):
                initialAge = 0
                print "Age is not valid, setting age to 0."

    The above snippet should come before assigning self.initialAge = self.initialAge so that if it doesn't meet the condition then it won't be assigned and the error can be logged back.

    class Person:
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
            if (initialAge < 1):
                print("Age must be above 0")
            self.initialAge = initialAge
    The above code snippet will make sure the condition is met first before assigning self.initialAge = initialAge.
    Michael Gichia
    @michaelgichia
    To increment age then call the increment method .
    in your case it is Person().yearPasses().

    If you look at your method

        def yearPasses(self):
            # Increment the age of the person in here        
            self.initialAge = self.initialAge + 1

    you have not returned self.initialAge thus you won't get any value.

        def yearPasses(self):
            # Increment the age of the person in here        
            self.initialAge = self.initialAge + 1
            return self.initialAge
    Michael Gichia
    @michaelgichia

    I would recommended you revisit Class/Objects and OOP.

    The tutorials are well explained and you will understand better.

    clarch
    @clarch
    simple and nice, thanks :+1:
    Walusimbi Mahad
    @andela-engmkwalusimbi
    It is globally available in class. @michaelgichia @clarch it's not global per say, it's set as an instance
    class Person:
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
            if (initialAge < 1):
                print("Age must be above 0")
            self.initialAge = initialAge
    @michaelgichia this won't deny self.initialAge to equal to whatever value that a user passed in.
    Perhaps you might want to do it like the below snippet
    class Person:
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
            if (initialAge < 1):
                        initialAge = 1
                        # may be you change the message printed out
                        print("Age must be above 0 ")
            self.initialAge = initialAge
    Walusimbi Mahad
    @andela-engmkwalusimbi

    For this method you actually don't need to return self.initialAge . Since it's an attribute of that class, you can access initialAge by using the object/instance of the class
    ```def yearPasses(self):

        # Increment the age of the person in here        
        self.initialAge = self.initialAge + 1
        return self.initialAge```

    i.e.

    mahad = Person(2)
    mahad.yearPasses()
    print mahad.initalAge
    Michael Gichia
    @michaelgichia
    @andela-engmkwalusimbi thanks for the clarification.
    Walusimbi Mahad
    @andela-engmkwalusimbi
    @michaelgichia thanks for the good work
    clarch
    @clarch
    This message was deleted
    clarch
    @clarch

    @andela-engmkwalusimbi ahh thanks for the clarification mahad. Ok so tell me why this also works

    class Person:
        age=0
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
             if (initialAge < 1):
                 print("Age must be above 0")
             self.age = initialAge
          def  yearPasses():
              self.age = self.age + 1

    shouldn't we reference class variables as in this case:

    def yearPasses():
        Person.age = Person.age + 1

    or is it the same thing and why or how is it interchangable

    sorry for mine I assigned self.age = initialAge before the if statement in init like so
    class Person:
        age=0
    
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
            self.age = initialAge
             if (self.age < 1):
                 print("Age must be above 0")
    
          def  yearPasses():
              self.age = self.age + 1
    Michael Gichia
    @michaelgichia
    @andela-engmkwalusimbi cheers there.
    carinaP2011
    @carinaP2011
    hi guys ... i have a question... if i connect via Remote Desktop will anyone using the server see what i am doing like teamviewer?
    Martin Cornel
    @ocornel
    @carinaP2011 No
    Anthony Tirop
    @Antoh1
    @clarch @andela-engmkwalusimbi why do you guys put the conditional statement within the init/constructor function!?, shouldn't it be handled by any method using the variable!! just concerned about code readability/clarity.
    jnmunyua
    @jnmunyua
    @carinaP2011 Yes its possible for them to see.
    clarch
    @clarch
    @Antoh1 I am not sure I understand what you mean could you modify the code and share so as to see it clearly
    clarch
    @clarch
    @carinaP2011 and @jnmunyua if you are referring to Windows Remote Desktop the other person cannot see what you are doing , although the server allows parallel sessions, that is two people can be logged in at the same time, they cannot view what the other person is doing, well unless one of you uses teamviewer but that would require authorization from the other person :smile:
    clarch
    @clarch

    @Antoh1 I think I understand your statement, do you mean that for every object created from Person() we put the if statement inside the object?
    Well I did it this because I want to check the validity of the age, that self.age>1 always and since this is constant for all the objects that will be created I put it in the init otherwise I would have to put

    if (self.age < 1):
                 print("Age must be above 0")

    inside every object created which means more lines of code and it will be repetitive, I don't know if there is a better way to do it and why it would not be readable kindly share. Hope I helped some :smile:

    Anthony Tirop
    @Antoh1
    This message was deleted
    Anthony Tirop
    @Antoh1
    class Person:
        age=0
        def __init__(self,initialAge):
            # Add some more code to run some checks on initialAge
             self.age = initialAge
          def  yearPasses():
              if initialAge <1:
                  print("Age must be above 0")
              self.age = self.age + 1
    @clarch this is what I actually meant. the condition should be in you class method not really when you instanciating your object
    def yearsPasses(self):
    My approach based on the python class's conventions
    clarch
    @clarch
    @Antoh1 oh yeah that makes sense. Although I still think it depends, for instance if def yearPasses() is the only method that you are going to create that uses self.age then having the if statement inside the method would make sense and so for my code it would do but what happens when I want to create several other methods that use self.age, say like def monthPasses() and def decadePasses() how then would I go about it efficiently without repeating the if statement every time I create one.
    Anthony Tirop
    @Antoh1
    @clarch your idea is awesome too, but I am concerned with a newbie going through your code having in mind what the init function does, so to me it might bring some confusion to the newbie. Yes it works, but to enable clean code. You can define the statement and save it in a variable to be reused in other functions.
    Am not specifically talking about that single if statement, what if you encounter several like those which will tempt you to put all of them in the init function!! the constructor might eventually lose its scope.
    clarch
    @clarch
    This message was deleted
    clarch
    @clarch

    @Antoh1 yea that works I had missed that. We can store the if statement in another method instead like so, as many as we want, this would be more readable

    class Person:
    
        age=0
    
        def __init__(self,initialAge):
             self.age = initialAge
    
        # Check whether age is greater than 1
        def  age_validity():
             if initialAge <1:
                  print("Age must be above 0")
                  initialAge = 0
    
        def  yearPasses():
             self.age = self.age + 1

    @/all anyone with any thoughts hehe

    taintunde
    @taintunde
    @clarch ?