2016-03-04 45 views
0

我想調用類的init函數中的類方法。我將一個字符串傳遞給函數,但錯誤顯示它是一個無類型的對象。AttributeError:'NoneType'對象沒有屬性'lower'

class UserSettings(object): 
    """Value object representing a user's settings.""" 
    def __init__(
      self, user_id, email, username=None): 
     self.user_id = user_id 
     self.email = email 
     self.profile_picture_data_url = self.fetch_gravatar(email) 


@classmethod 
    def fetch_gravatar(cls, email): 

     base_url = "http://www.gravatar.com/avatar/" 
     avatar_url = base_url + hashlib.md5(email.lower()).hexdigest() + "?" 
     avatar_url += urllib.urlencode({'d':'retro', 's':str(AVATAR_SIZE)})    
     return avatar_url 

這裏的錯誤:

Traceback (most recent call last): 

    File "/home/travis/build/oppia/oppia/core/domain/user_services_test.py", line 78, in test_invalid_emails 

    user_services.get_or_create_user('user_id', email) 

    File "/home/travis/build/oppia/oppia/core/domain/user_services.py", line 297, in get_or_create_user 

    user_settings = _create_user(user_id, email) 

    File "/home/travis/build/oppia/oppia/core/domain/user_services.py", line 284, in _create_user 

    preferred_language_codes=[feconf.DEFAULT_LANGUAGE_CODE]) 

    File "/home/travis/build/oppia/oppia/core/domain/user_services.py", line 55, in __init__ 

    self.profile_picture_data_url = self.fetch_gravatar(email) 

    File "/home/travis/build/oppia/oppia/core/domain/user_services.py", line 129, in fetch_gravatar 

    avatar_url = base_url + hashlib.md5(email.lower()).hexdigest() + "?" 

AttributeError: 'NoneType' object has no attribute 'lower' 
+0

請問您可以添加完整的錯誤輸出和fetch_gravatar調用嗎? – Altoyyr

+0

'self.email.lower()'也許?不知道你在哪裏調用'fetch_gravatar',所以我們只能猜測。 – Lafexlos

+0

@Lafexlos:無論如何,電子郵件正在傳遞給函數 –

回答

1

AttributeError: 'NoneType' object has no attribute 'lower'

這告訴你,你正試圖閱讀單項對象None名爲lower的屬性。

現在,您在email的方法調用中使用lower。因此,emailNone

0

您必須調用方法的值爲None,或者參數值爲None

# calling the class method: 
>>> UserSettings.fetch_gravatar('[email protected]') 
'http://www.gravatar.com/avatar/b418773a2c51fb9777a1648346fa7394?s=16&d=retro' 

# create instance, call method: 
>>> user_settings = UserSettings(user_id=1, email='[email protected]') 
>>> user_settings.fetch_gravatar('[email protected]') 
'http://www.gravatar.com/avatar/e84a7df193a44f643668b74a2bbfdde6?s=16&d=retro' 

# calling with a No arguments gives TypeError 
>>> UserSettings.fetch_gravatar() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: fetch_gravatar() takes exactly 2 arguments (1 given) 

# calling with None gives your error: 
>>> UserSettings.fetch_gravatar(None) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 11, in fetch_gravatar 
AttributeError: 'NoneType' object has no attribute 'lower'