1

Okay, so I am working on creating some commands in django, but I came across a intriguing issue with regards to my code here...

from django.core.management.base import LabelCommand
from optparse import make_option

class Command(LabelCommand):
    requires_system_checks = False
    can_import_settings = True

    args ="[none]"
    label = "person's name"

    option_list = LabelCommand.option_list + (
         make_option('--cap',
         action='store_true',
         dest='capitalize',
         help= 'Tells Django to capitalize the name'),
        )

    help = "Runs a shell output that writes out 'Hello' and the specified name"

    def handle_label(self, name, **options):
        if options.get('capitalize', False):
            name = name.capitalize()

        print ("Hello %s!" % name)

What bothers me revolves around the handle_label def in the latter part of the code, and this apparent difference between self and object inputs. I thought that self was the instance inherited from the main class (in this case, Command). Im confused about this, because if I am actually inputting an object (in this case, name) into this method, then why do I need self? I am not referencing it here, why should I need to input this as a parameter? Perhaps Im not understanding this method very well. Either way, can someone help me clarify these differences? Thanks.

JellisHeRo
  • 537
  • 1
  • 7
  • 17
  • There's a lot of confusion in this question, and I don't really know how to answer it. *All* instance methods need to take `self` as a parameter. – Daniel Roseman Aug 01 '15 at 21:26
  • you are right; `handle_label` does not use the `self` argument and could therefore also be a static method. – hiro protagonist Aug 01 '15 at 21:39
  • Thanks for your responses! I figured it out, and I am kind of kicking myself over it lol. I used to be more involved with Java programming, but didnt once think about Python's "self" comparison to Java's "this". The difference is a matter of being implicit (Java) or explicit (Python) in implementation. Both are used to access the variable associated with the current instance, but in Java, I dont have to input "this" in order to use it: its implicitly included, whereas Python makes us input 'self' in order to use 'self'...but I still wonder if it is worth doing so if Im not even using it. – JellisHeRo Aug 01 '15 at 23:42

0 Answers0