Python Method Parameters and self

Sdílet
Vložit
  • čas přidán 17. 03. 2016
  • Looks at the parameters of a method as defined in a class but used in an instance of the class.

Komentáře • 41

  • @lakeguy65616
    @lakeguy65616 Před 2 lety +2

    perhaps the best tutorial for Python I've ever seen. Bravo!

  • @jonathantieken
    @jonathantieken Před 7 lety +18

    Thank you SO MUCH. It's amazing how understanding one simple thing - like the fact that the object's id is the first parameter passed to every method - completely opens my eyes to all the walls I've been hitting for months now. Good grief - this is the first and only explanation I've seen in almost 2 dozen videos and articles that ACTUALLY EXPLAINS THAT ONE CRUCIAL DETAIL!
    I suppose I might have reasoned that out if I had internalized the Pythonic mantra that "Everything Is An Object", but unsurprisingly I simply missed it.
    Cheers, mate!

  • @simonj3342
    @simonj3342 Před 7 lety +6

    WHO ARE YOU AND WHY AM I JUST FINDING OUT ABOUT YOUR VIDEOS?
    Your explanations are top-notch! Infinitely better than so many other videos or tutorials. Thank you so much JPJ. I've been struggling with these concepts which turned out to be incredibly simple upon viewing your videos.

  • @ssjc02
    @ssjc02 Před 8 lety +4

    If every computer tutor out there explained things clearly as you do we'd all be programmers. Thank you so much man. This playlist has really helped me put things in to perspective. Please do more tutorials such as computer networking.
    Thank you.

    • @johnphilipjones
      @johnphilipjones  Před 8 lety

      +John Collins Thank you for your positive comments. I am committed to making Python videos at the moment but will eventually get around to videos on networking.
      Best wishes
      Phil
      (John Philip Jones)

  • @JohnBillot
    @JohnBillot Před 8 lety +2

    Thank you Philip. I have been confused by this "self" issue for a long time and have had to write in a "parrot fashion" way without knowing why I'm doing this - something I hate doing.
    This has cleared the whole issue up for me for which I'm very grateful. Thanks again.

  • @haezlitt
    @haezlitt Před 7 lety +4

    John, I can't thank you enough, thank you for taking the time to make these thoroughly well thought out videos.

  • @nickmiller5805
    @nickmiller5805 Před 8 lety +5

    This topic is one that I've struggled with for quite some time now. It all makes sense after watching your video though. I appreciate you taking the time to make all of these great Python videos. Cheers!

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

      Thank you for your positive comments. Glad the videos are helping.
      Best wishes
      Phil
      (John Philip Jones)

  • @sanaullahkhanhassanzai8432

    Thank you so much for this video. The previous videos now makes complete sense. Everything is crystal clear now. the last bit of the video where you showed that the class can be accessed directly without the self used in the method made everything clear about the need of self in the first place. great explanation. thank you. keep it up. God bless you.

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

    great examples of passing values to parameters

  • @SusanAmberBruce
    @SusanAmberBruce Před 8 lety +4

    This is the lesson I have been waiting for, so thanks Philip.

  • @franklehmann3449
    @franklehmann3449 Před 8 lety +2

    Thanks for your well-thought explanations!

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

    Thanks, was much helpful

  • @RajSingh-hv2ij
    @RajSingh-hv2ij Před 7 lety +6

    Exellent Explanation

  • @ddddsdsdsd
    @ddddsdsdsd Před 9 měsíci +1

    Very good work!❤️

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

    Wow , wow, wow.

  • @mscorpio1111
    @mscorpio1111 Před 4 lety

    Great video. I agree with all the positive comments here (which seem to be the most or all of them). I do want to add a bit of constructive (hopefully) criticism however. While I understand that people are different and everybody learns at their own pace, the video has a LOT of repetition. Not a little but really a lot, stating the same thing over and over again. I did learn from the video, and I appreciate the very clear and exhaustive explanations, but to my personal taste it gets a bit too much. The same could have been done more effectively, i.e. the ratio "information/time" could have been much higher. Also, there is quite a bit of redundancy between different videos (I've only watched two so far). At some point, I actually had to do the fast forward to make it work for me.
    Posting this feedback in hope it can be useful. Again, the video is great and I thank you for it.

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

      Hello Michael, Thank you for the feedback. I think your comments are correct for a subset of students. However, for the videos I have produced to date it has been a deliberate policy. I wanted the videos to help the beginner who may be struggling with programming. Over the years I have seen many very able students just give up. Repetition for a struggling student works during the introductory parts of a language. I also often repeat information from a slightly different perspective to highlight the nuances of the language which is often missed by the viewer. The overlap between videos exists as many are designed to be viewed in sequence. Consequently, I am often reviewing work from a previous video.
      I have now almost completed the introductory features of the language. The more advanced features will result in 'tighter' videos.
      At the moment I am writing exercises for the videos which should (hopefully) show the advantage of the video style.
      The videos are better organized on the website where you will also find the exercises (I have finished these for section 1 and 2). I will be uploading section 3 exercises later this week. The website is at www.PythonByteSize.com
      Best wishes
      Phil

  • @andrewwebster5060
    @andrewwebster5060 Před 7 měsíci +1

    I have never really understood the concept of 'self' - UNTIL now! that said I am still unclear as to why it cannot be transparent to the programmer. I assume this in a requirement of more advanced programming

  • @GreatWhiteNiko
    @GreatWhiteNiko Před 8 lety +3

    John Phillip Jones,
    Last night I watched about 15 of your videos. Been watching some of them in a random fashion in the last month or so.
    Last night I had the "aha" moment that I've been waiting for years. I even worked as a computer programmer for a few years and did not understand what I was doing. That was possible because of the chaotic way the software was being created and maintained. I attribute my personal breakthrough last night to your style of presenting the subject.
    THANK YOU SO MUCH!
    Please keep up the good work!
    THANK YOU AGAIN!!!
    Please let me know how I can contact you directly?

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

      Thank you for your positive comments. I can be contacted directly via my website at:
      www.pythonbytesize.com/contact.html
      Best wishes
      Phil
      (John Philip Jones)

    • @tupG
      @tupG Před 7 lety +4

      I couldn't agree more. I'm not a professional programmer but have been dabbling in and out on a semi-professional level. I was able to use classes, created objects for years but never 'really' understood the details. Most authors of tutorials throw their arms into the air as soon as they have to discuss OOP and start with a statement along the lines:
      "now, this is going to be tricky ....... you gonna struggle with this. Just try to understand my explanations and punch in the script examples"
      Your examples with the identifiers right from the start made it fairly easy by comparison.
      A VERY BIG THANKS from my side too.

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

      thank you glad the videos are helping
      best wishes
      phil

  • @zeeshanakram8751
    @zeeshanakram8751 Před 6 lety

    Sir! there is a question:
    is this specific word "self" must for fixing the problem, or we can use any other word of our choice ? I tried some other words even letters and they also fix the error.Using python 3.6.5.
    Thanks a lot.

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

      It is standard practice to use 'self' but you can use other words instead of 'self' is not a Python keyword. However, my advice is always use 'self'.
      Regards
      Phil

  • @PritiyindaHonne
    @PritiyindaHonne Před 6 lety

    HI
    Can You Please explain what will happen if we give same for formal parameters as actual parameters
    example:
    def test(a):
    print a
    a = [10]
    test(a)

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

      def test(a): print(a) print('Inside the function',id(a))a=[10]print('Before the call',id(a))test(a)print('After the call',id(a))
      The above code shows that the id for the actual and formal
      parameter is one and the same. Essentially, the ‘pointer’ to the object is passed. This is outlined in the following video:
      czcams.com/video/hDmnu1qGfkw/video.html
      Best wishesPhil

  • @Lennardish
    @Lennardish Před 3 lety

    Do I understand it well that the instance of the class "my_object = DemoClass() " has to be seen as the 'first' argument of the three in "add_them" ? Has the word "self' been deliberitely chosen?

    • @johnphilipjones
      @johnphilipjones  Před 3 lety +3

      self will receive the identity (id) of the instance of the class (i.e. the object). Every created object will have a unique id. self has to be present so each object can be identified i.e. every object will have its own copy of self.
      self is NOT a keyword of the language. However, every programmer has agreed to use self.
      Best wishes
      Phil

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

      @@johnphilipjones I do understand it now. I am 78 years of age and used to program in Visual Basic 6.0. Python is in my opinion so much "better" thought of, and really, you are an EXCELLENT teacher. Never had a better one in my life, I really mean this. Thank you. I am typing in all your examples to get it "in my fingers" :)) Best Wishes Mr. Jones,
      Paul Miedema
      Netherlands

    • @johnphilipjones
      @johnphilipjones  Před 3 lety

      Hello Paul,
      Thank you for your generous comments. It is very good to know that my videos are helping people learn Python.
      Best wishes
      Phil

  • @hiteshgupta968
    @hiteshgupta968 Před 3 lety

    Your video is amazing i just have one simple doubt
    # this code will not take self parameter and still work it
    class Human:
    def animal():
    print('This is an animal')
    print(Human.animal())
    # can you please explain it sir

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

      Self is used when you know that you intend to produce an instance of the class (i.e. an object of the class). In your code you are using the class directly hence the self is not required. However, I would advise that you always use the class as a template from which objects are derived. This will require the use of self.
      Best wishes
      Phil

    • @hiteshgupta968
      @hiteshgupta968 Před 3 lety

      @@johnphilipjonesgot it👍
      love from india you are the best teacher i found so far 😍

  • @jvsnyc
    @jvsnyc Před 3 lety

    At first I didn't get why you were so averse to having class methods in addition to instance methods. They are very important for instance in Java. However, Java doesn't allow you to have any functions that AREN'T members of a class -- they aren't "first class objects". So if something isn't an instance method, just leave it a procedural-style function not belonging to a class. Of course, there are other uses for class or static methods in places like C++, despite also being able to write free-standing, procedural functions that are not a member of a class. I will pay attention to how other people who seem to know Python very well and use it effectively feel about having functions that are not instance methods, but do belong to some class, as we would commonly see in C++ and even more commonly in Java (since they have no choice in the matter!)
    Does this also mean you almost never make use of modules containing free-standing autonomous functions not belonging to any class?

    • @johnphilipjones
      @johnphilipjones  Před 3 lety

      I started out as a machine code programmer then moved onto C and arrived at programming in Python via various other languages (e.g. Pascal, VB .NET, Java). Early in my career I followed the procedural paradigm and designed my code using Structures charts which followed on from data flow analysis (Yourdon and deMarco).
      When I started coding in objects I was as confused as hell and would often think ‘what is the point of this approach’. I then moved into object-oriented analysis and design using the Unified Modelling Language (UML). This approach was based on concepts analysis which ultimately led to Classes.
      I then worked my code to match the class diagrams and collaboration diagrams that showed how the objects communicated. Consequently, I would strive to only have fully encapsulated classes. My code would also only have messages being sent to objects (i.e. not calling functions/procedures as with procedural coding).
      I very much like Python and it does serve more than one paradigm (e.g. OO and Procedural). In my view this is both a strength and a weakness. A strength because it can match procedural design and OO designs. The trouble is you can flip between both and as a result you are not really following a consistent life cycle for each of the paradigms. If you are following OO then you should avoid class functions and only code for methods that are encapsulated within the class and only available when an instance of the class is created. This will match the UML collaboration diagrams (now called communication diagrams).
      Although I grew up using the procedural paradigm, I now find it much easier to use OO especially as I design using UML.
      Having said this I am by training an engineer and I will follow a design until it does not work as expected. At that point I am happy to use anything that works hence my liking for Python. It will allow me to break perceived OO Software Engineering principles and break into the procedural paradigm if I find a module with lots of functions that work. Of course, this makes testing the software a pain.
      Not sure this answers your questions but hopefully it will give an insight into my approach to the videos. You will notice in the playlists I cover procedural and OO approaches to the program snippets. I will use procedural if I am introducing a new topic area and move towards OO. My reasoning behind this is experience has shown me that OO is another layer of complexity that beginners to a language find difficult to grasp. It took me quite a long time to appreciate the OO approach, but I would not go back to my early procedural designs (unless they save me time because they still work).
      Best wishes
      Phil

    • @jvsnyc
      @jvsnyc Před 3 lety

      @@johnphilipjones It does. Lunch is about to start, but I just want to mention in OOPS C++, you still often have static methods that take more than one object, something like best(MyClass o1, MyClass o2, MyClass o3) ... my professor once described those as being class/static instead of instance methods because "no single one of them plays a starring role"