0

I've started working with python recently and am totally confused.

I have the following class:

class Vault:

  def __init__(self):

    # used to mock collection (table) of ads
    self._ads = [ {
        'id': '00000000-0000-0000-0000-000000000000',
        'date': str(datetime.now().strftime('%Y%m%d')),
      'time': str(datetime.now().strftime('%H%M%S')),
        'source': 'chron.com',
        'advertiser': 'itunes.apple.com',
        'width': 300,
        'height': 250
    } ]
  def get_ad_by_d(self, d):
    myDate = getTodayDate()
    ads = [ ad for ad in self._ads if ad['date'] == d ]
    if len(ads) == 0:
      return None
    elif len(ads) >= 1:
      return ads[0]
  def getTodayDate():
    return str(datetime.now().strftime('%Y%m%d'))

However when I call it I get the following error:

NameError: global name 'getTodayDate' is not defined

Why can I not access another function in the same class? I wrote this code in textMate, however I've never had issues accessing neighboring functions in the same class when working in Eclipse. Am I missing something?

  def getTodayDate(self):
    return str(datetime.now().strftime('%Y%m%d'))
  def getTodayTime(self):
    return str(datetime.now().strftime('%H%M%S'))

Works to solve the above problem however implementing it in init fails (Found thanks to answers):

  def __init__(self):
    myDate = getTodayDate()
    myTime = getTodayTime()
    # used to mock collection (table) of ads
    self._ads = [ {
        'id': '00000000-0000-0000-0000-000000000000',
        'date': myDate,
      'time': myTime,
        'source': 'chron.com',
        'advertiser': 'itunes.apple.com',
        'width': 300,
        'height': 250
    } ]

I have a similar error that is not solved by adding self:

File "/Users/tai/Desktop/FlashY/flashy/repository/mock.py", line 10, in __init__
    myDate = getTodayDate()
NameError: global name 'getTodayDate' is not defined

solution which was in comments:

  def __init__(self):
    myDate = self.getTodayDate()
    myTime = self.getTodayTime()
    # used to mock collection (table) of ads
    self._ads = [ {
        'id': '00000000-0000-0000-0000-000000000000',
        'date': myDate,
      'time': myTime,
        'source': 'chron.com',
        'advertiser': 'itunes.apple.com',
        'width': 300,
        'height': 250
    } ]
Tai
  • 1,206
  • 5
  • 23
  • 48

1 Answers1

5

Use self in your method def getTodayDate(self): andmyDate = self.getTodayDate()

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Oh, interesting. Why have I not needed self in the past? Is it because I need functions using self to match? Can you explain how self works in python? – Tai Jul 17 '14 at 21:23
  • Every method of a class in Python that's not a `@classmethod` or a `@staticmethod`, is passed a reference to the instance of the class as the first argument. You can call this variable whatever you want, the convention is to always call it `self`. – OregonTrail Jul 17 '14 at 21:27
  • @TaiHirabayashi, there is a thorough explanation here http://stackoverflow.com/questions/2709821/python-self-explained – Padraic Cunningham Jul 17 '14 at 21:31
  • awesome :) I'm primarily Java and never encountered this issue before. Thanks again – Tai Jul 17 '14 at 21:33
  • Actually I'm seeing the same issue. It worked for my call in get_ad_by_d. However if I do: def __init__(self): myDate = getTodayDate() it has the same error of: global name 'getTodayDate' is not defined – Tai Jul 17 '14 at 21:38
  • myDate = self.getTodayDate() – Padraic Cunningham Jul 17 '14 at 21:39
  • self is confusing :S ok but wait then why does adding self to the getTodayDate(self) work to solve the problem in the other functions? Is it due to the init? – Tai Jul 17 '14 at 21:41
  • lol, why are you using `myDate = getTodayDate()` in the init method, you can refer to the methods anywhere in the class using `self.my_method` or `self.my_attribute`? – Padraic Cunningham Jul 17 '14 at 21:42
  • I was trying to debug... but yes thank you. I actually realized I needed self in both cases so I solved my previous question. Thank you! – Tai Jul 17 '14 at 21:44
  • No worries, it will get easier and certainly easier than Java! – Padraic Cunningham Jul 17 '14 at 21:45