SORU
13 AĞUSTOS 2009, PERŞEMBE


BaseException.mesaj Python 2.6 onaylanmamış

BaseException bir uyarı alıyorum.mesaj aşağıdaki kullanıcı tanımlı özel durum: kullanırken Python 2.6 önerilmiyor

class MyException(Exception):

    def __init__(self, message):
        self.message = message

    def __str__(self):
        return repr(self.message)

Bu uyarı

DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
self.message = message

Bu nesi var? Ben değiştirmek için admin uyarı kurtulmak için ne yapmak?

CEVAP
17 Mayıs 2011, Salı


Çözüm neredeyse hiçbir kodlama gerekli

  1. Sadece miras Exception özel durum sınıfı
  2. ve kurucu ilk parametre olarak mesaj geçmek

Örnek:

class MyException(Exception):
    """My documentation"""

try:
    raise MyException('my detailed description')
except MyException as my:
    print my # outputs 'my detailed description'

str(my) ya da pek şık my.args[0] özel mesaj erişmek için kullanabilirsiniz.

Arka plan

Python (2.6) daha yeni sürümleri bizim özel BaseException devralan Özel Durum Özel Durum sınıfları miras gerekiyor. Arka plan PEP352 nda ayrıntılı bir şekilde tanımlanmıştır.

class BaseException(object):

    """Superclass representing the base of the exception hierarchy.
    Provides an 'args' attribute that contains all arguments passed
    to the constructor.  Suggested practice, though, is that only a
    single string argument be passed to the constructor."""

__str__ __repr__ zaten anlamlı bir şekilde uygulanır özellikle tek bir arg (ileti olarak kullanılabilir) diye.

__str__ __init__ uygulama tekrar ya da başkalarının önerdiği gibi _get_message oluşturmak gerekmez.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Animation Workshop

    Animation Wo

    8 NİSAN 2010
  • ecf150king

    ecf150king

    20 Ocak 2006
  • xXGAMERrs_Xx

    xXGAMERrs_Xx

    31 Temmuz 2014