Closed Thread
Results 1 to 3 of 3

Thread: optional function arguments

  1. #1
    random guy is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    48
    Rep Power
    0

    optional function arguments

    I have an interesting issue with having an optional argument to a function that is an property of a class:

    I want to do the following

    def addStockToMarket(self, stockTicker, startDate=self.tradingDay):

    where tradingDay is a property of the class that is set when the class object is constructed. the issue is that it doesnt like that i am doing that because i guess it doesnt know what "self" is at that point in the function definition since it is getting self passed into it.

    is there any way around this issue other than making seperate functions for each set of arguments?
    Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
    add to my rep. its quick and easy and definitely wont steal your girlfriend.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    saeras's Avatar
    saeras is offline Learning Programmer
    Join Date
    Jan 2010
    Location
    Sweden
    Posts
    67
    Rep Power
    8

    Re: optional function arguments

    You could set startDate=None or something and then make an if check. example:
    Code:
    def addStockToMarket(self, stockTicker, startDate=None):
        if startDate == None:
            startDate = self.tradingDay
    I don't know if doing this is generally accepted though.

  4. #3
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: optional function arguments

    Quote Originally Posted by saeras View Post
    I don't know if doing this is generally accepted though.
    In fact, that's the generally recommended method, and you can add more conditions there to verify the validity of the argument.
    Code:
    def addStockToMarket(self, stockTicker, startDate=None):
        if startDate == None or not isValidDate(startDate):
            startDate = self.tradingDay
    Since "None" probably wont be a valid date, you can just write:
    Code:
    if not isValidDate(startDate):
        startDate = self.tradingDay

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MCQ: variable declared in arguments of this function
    By jackson6612 in forum C and C++
    Replies: 8
    Last Post: 06-05-2011, 07:14 AM
  2. how to write function for variable arguments
    By sai in forum Managed C++
    Replies: 1
    Last Post: 01-03-2011, 10:10 AM
  3. too few arguments to function ‘fgets’
    By theCnewbie in forum C and C++
    Replies: 5
    Last Post: 09-12-2010, 12:49 PM
  4. Optional Closing Tags in HTML
    By Big Mountain in forum HTML Programming
    Replies: 18
    Last Post: 05-18-2008, 07:34 AM
  5. Optional Closing Tags in HTML
    By Bodyag in forum HTML Programming
    Replies: 2
    Last Post: 03-11-2007, 11:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts