Python getter and setter Methods

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • This video looks at whether 'setter' and 'getter' methods are needed in Python. It also acts as a review and summary of the last three videos in the Python playlist.

Komentáře • 46

  • @Mrjarnould
    @Mrjarnould Před 7 lety +21

    You are the best Python tutor on CZcams!

  • @TheArchaeus6
    @TheArchaeus6 Před 6 lety +15

    Hundreds of Python tutorials... You are the Superior teacher. Hands down the best explanations for creating solid associations while learning. Thank you for your efforts.

    • @johnphilipjones
      @johnphilipjones  Před 6 lety +2

      Thank you for your generous comments they are appreciated. Please consider sharing the channel as it helps keep the video series free to view.
      Best wishes
      Phil

  • @crossfarm4146
    @crossfarm4146 Před 4 lety +3

    I've been doing Python for about 5 months now, I bought a Udemy Course, found websites, searched youtube. Couldn't find anything that could explain classes to me, and that was the only thing that was really allowing me to be fluent in the basics of Python. I found these videos, and I'm confident that I'm going to have to basis to do anything in python programming, after I complete all of these videos. Amazing work!

    • @johnphilipjones
      @johnphilipjones  Před 4 lety +2

      Thank you for your positive comments I'm glad the videos are helping Best wishes Phil

  • @martinlutherkingjr.5582
    @martinlutherkingjr.5582 Před rokem +2

    For beginners this style of teaching is incredibly useful and innovative.

    • @johnphilipjones
      @johnphilipjones  Před rokem +1

      Thank you for your positive comments.
      Best wishes Phil

  • @Lennardish
    @Lennardish Před 3 lety +4

    Extremely clear Mr Jones, Thank you. By far the best tutorial that I have visited so far on CZcams (I abandoned the others :)

  • @juliamason6867
    @juliamason6867 Před 3 lety +1

    It finally makes sense! I could cry! 😭

  • @ashtonsimmonds7042
    @ashtonsimmonds7042 Před 5 lety +2

    Professional and clear. I am going to use this in my classroom.

  • @neonalton
    @neonalton Před 7 lety +1

    It makes sense in the world of Jones, you make programming fun, thanks.

  • @ashishhk
    @ashishhk Před 6 lety +3

    Well executed
    Must have put in a lot of work

  • @davidochieng1443
    @davidochieng1443 Před 2 lety +1

    wonderful... wonderful.... wonderful👏

  • @msuegajnriorpenda9745
    @msuegajnriorpenda9745 Před 6 lety +2

    Cool stuff! You are awesome. I understood every bit.

  • @Fullrusher
    @Fullrusher Před 4 lety

    This is the only one of your videos I don’t understand, classes , getters and setters confuse me also functions lol I’m quite new forgive me great video as always tho your pace and detail are legendary!

    • @johnphilipjones
      @johnphilipjones  Před 4 lety +1

      Think of an object as an entity that is self-contained. Within the object there is behaviour and attributes where the behaviour and attributes are defined in the class.
      Think of a class as a factory that produces the objects. If you want the object to perform a task (e.g. calculate the charges on an overdraft) you send it a message to invoke one of it behaviours. These behaviours are small segments of code (i.e. methods which is like a function) contained within the object.
      An attribute again contained within the object is essentially a variable (e.g. it stores the current debit interest rate).
      At some point you will need to set the current debit rate to a value. When you wish to know what the current debit interest rate is you need to obtain it.
      Whatever the debit interest rate is it will be stored within the object.
      One way to set the debit interest rate to a value is to invoke the setter method (which allows you access to the debit interest rate).
      To find out what the current debit interest rate is you invoke the getter method which again allows you access).
      Getter and setter methods are typical for most object orientated languages. It is also typical terminology used in UML design diagrams. For this reason I have shown how you can use getter and setter methods in Python.
      However using such methods is not regarded as pythonic. But many programmers coming from another language to Python often use this approach.
      A strong feature of object oriented programming is to hide data within attributes of an object. Only allowing access to such data via special mechanisms. This is often achieved in other languages using the reserved word 'private'. Python has not adopted this approach it is possible to obtain direct access to an attribute within a Python object without a special mechanism. However, you can as a programmer decide to use getter and setter approach. This would be a decision for you as an individual programmer and for a programming house. Whatever the programming house decides upon you would be forced to follow suit. Pythonic programmers would not use getters and setters.
      Best wishes Phil

    • @Fullrusher
      @Fullrusher Před 4 lety

      John Philip Jones this was such a good explanation I feel like if I read it enough times I’ll get it completely

  • @boxicool
    @boxicool Před 7 lety +1

    Amazing python Tutor, soo clear!! Thanks!

  • @MaxGoddur
    @MaxGoddur Před 7 lety

    This is probably not important as I understand how it works as you describe but had to ask this question.
    When we create the instance and pass the :
    customer = Account (20, "Rita Jones")
    It actually is going to :
    def __init__(self, balance, name):
    and you would think it was going to:
    class Account(balance,name):
    If you were new to this, as am I, you would not see how the name and age were getting to the __init__() as there is no path to follow in the code.
    Yet another great video thank you each video is like a holiday.

    • @johnphilipjones
      @johnphilipjones  Před 7 lety

      This is a good question. The diagrams I use to explain classes and objects are very much a model that I have effectively created to hide the underlying mechanisms of the Python language. They are based on the messaging model of UML. Therefore they should not be taken too literally the model is a good approximation. Python uses namespaces and dictionaries to ‘control’ the runtime. It is my view that UML does not support the Python ‘model’ well hence my approximation model. But to answer your question from the perspective of the model I would regard the __init__ as being defined by the class with each object being given their own copy with the self parameter holding the id of each instance. In other words the __init__ method knows which object it belongs to because of the id it is passed.
      Best wishes
      Phil

  • @dragangosevski6425
    @dragangosevski6425 Před 7 lety +1

    I have watched 4 tutorials about OOP, i must admit this one is the best. Mr. can you make something like this but with dictionary or list? Really good work sir.Thank you very much.

  • @amitg8223
    @amitg8223 Před 7 lety +1

    Excellent tutor. Thanks

  • @marcc1179
    @marcc1179 Před 4 lety +1

    nice animation!

  • @deservewatching-5481
    @deservewatching-5481 Před 5 lety +1

    creative as always... thanks

  • @juliamason6867
    @juliamason6867 Před 3 lety +1

    OMG thank you!!!!!!!!

  • @josuewuezo7289
    @josuewuezo7289 Před 5 lety

    New to Python and working on this topic currently to complete programming course, your explanation was great, thanks a lot
    hopefully you can create hundreds of tutorials, by the way, anything related to MySQL and JavaScript? Kind regards

    • @johnphilipjones
      @johnphilipjones  Před 5 lety +1

      Thank you for the positive comments, Video on MySQL are in the pipeline.
      Best wishes
      Phil

  • @LonglongFeng
    @LonglongFeng Před 7 lety

    always best of the best...
    and I checked your personal website, and looking for some exercises for Class and Object designing, but I couldn't find any. I wonder if you are going to make some in the future? It's ok for me to pay the tuition to your courses :)
    keep on your splendid teaching work, man !

    • @johnphilipjones
      @johnphilipjones  Před 7 lety

      Once I get to around 200 Python videos (104 at the moment) I will be writing some more exercises. This will be in around 12 to 24 months time. All of the videos are free to view so no tuition fees needed :-).
      Although you could help sponsor the work on Patreon or via website - all monies go towards maintaining the website, updating hardware and software needed to make the videos etc.Links are below:
      www.patreon.com/user?u=3538393
      www.johnphilipjones.com/donate.html
      Best wishes
      Phil

  • @ko95
    @ko95 Před 4 lety +1

    Hello John, i understand that we should strive to use properties in python. Is this assumption correct?
    Instead of using setters and getters should we use @property and @x.setter decorators ?

    • @johnphilipjones
      @johnphilipjones  Před 4 lety +2

      Hello Kevin, You are correct decorators are the best approach with Python. The setters and getters are used with many other languages. It is ok to use getters and setters with Python if you (and your programming team) can keep their discipline when writing code. However, Python does not hide data in the way other languages do (for example other languages use the keyword Private). Therefore decorators are a better approach.
      Best wishes Phil

  • @jpizzle71477
    @jpizzle71477 Před 7 lety +1

    keep em coming! Choo choo

  • @anoopkg8681
    @anoopkg8681 Před 5 lety +1

    great

  • @nasrullahturke4480
    @nasrullahturke4480 Před 5 lety

    I have few questions here John
    Under init we have 2 formal parameters balance which to me keeps changing as and when balance is debited or credited. And name part is constant most of the time unless needed.
    When we created the instance of this class as below
    customer = Account (0,"Rita Jones")
    Self ID must have been created
    Now when we credit the account using method customer.credit the balance value changes. So does in this case the new object is created with replacing Old ID even though we used method. If that's the case then with each time changes in balance new object is created. Plz help me understand this.

    • @johnphilipjones
      @johnphilipjones  Před 5 lety

      The id of customer will remain the same. Write a segment of code to print the id at various stages of the execution of the program and observe the id's
      Best wishes
      Phil

    • @nasrullahturke4480
      @nasrullahturke4480 Před 5 lety +1

      @@johnphilipjones Thanks John❤. I already checked it. ID remains same.

  • @mathguy3801
    @mathguy3801 Před 7 lety

    Thanks. Can you do a video on how you create these wonderful graphics/video that are in your tutorials?

    • @johnphilipjones
      @johnphilipjones  Před 7 lety +1

      The animations are done in PowerPoint with additional animations overlayed in Camtasia. I am committed to making videos on Python for the next year but I will put the making of videos on how I make the videos in the pipeline. Also, I will need a more Powerful PC to do this which I will purchase next year. The following link has information on how I make the videos.
      www.pythonbytesize.com/educators.html
      Best wishes
      Phil
      (John Philip Jones)

    • @revellights1000
      @revellights1000 Před 6 lety +1

      I used PowerPoint and screen-recording software quite a bit in my prior life, so I appreciate all of the outstanding animation capabilities that PP has, and the ability to record your actions on PP. You've used some of the PP capabilities to greatly enhance your videos. I'm actually most impressed with the planning, design, and implementation of your videos, which can be really time consuming. As an Apple exec once said: Simple is hard. Besides your knowledge of Python, I think it's your planning and story-boarding each video, and division of the subject matter among the videos in the playlist, that are really impressive.

    • @johnphilipjones
      @johnphilipjones  Před 6 lety

      Thank you for your positive comments
      Best wishes
      Phil

  • @sanat1102
    @sanat1102 Před 5 lety

    Isn't it smarter to include all variables under __init__ since we can refer to it from inside any method at any later time?

    • @johnphilipjones
      @johnphilipjones  Před 5 lety

      Declaring variables in __init__() can indeed allow other methods declared in the class to be accessed from instances of the class. Of course depending on the design you may not want all methods to access all variables. It depends(in my view) on how the system has been designed using UML diagrams as to the scope of the variables available throughout the class and its instances. So it is not an easy question to answer. Also, I have covered the getter and setter approach here because it tends to fit with UML designs. Of course in Python many frown upon this approach. But that is a question and answer for another time.
      Best wishes
      Phil

  • @ashtonsimmonds7042
    @ashtonsimmonds7042 Před 5 lety

    I can't get this code to run. The error message says
    TypeError: Account() takes no arguments