जब पर अनिवार्य रूप से एक कस्टम प्रगणित प्रकार कार्यान्वयन काम कर रहा है, मैं एक स्थिति है जहाँ ऐसा लगता है मैं अलग int
और long
दोनों से अभी तक लगभग समान उपवर्गों प्राप्त करने के लिए, क्योंकि वे अजगर में अलग वर्गों रहे था में भाग गया। यह बहुत विडंबनापूर्ण लगता है क्योंकि दोनों के उदाहरण आमतौर पर एक दूसरे के लिए उपयोग किए जा सकते हैं क्योंकि अधिकांश भाग के लिए जब भी आवश्यक हो, वे केवल स्वचालित रूप से बनाए जाते हैं।दो अलग संख्यात्मक उपclass (int और long) होने से बचें?
जो मैंने ठीक काम किया है, लेकिन डीआरवाई की भावना में (स्वयं को दोहराएं मत), मैं यह सोचने में मदद नहीं कर सकता कि क्या कोई बेहतर नहीं है, या कम से कम एक संक्षिप्त, इसे पूरा करने का तरीका । लक्ष्य उप-वर्ग उदाहरण होना है जिसका उपयोग हर जगह किया जा सकता है - या जितना संभव हो उतना करीब - उनके आधार वर्गों के उदाहरण हो सकते थे। आदर्श रूप से यह स्वचालित रूप से int()
के तरीके के समान ही होना चाहिए जब भी यह पता चलता है कि किसी को आवश्यक है तो long
देता है।
यहाँ मेरी वर्तमान कार्यान्वयन है:
class NamedInt(int):
"""Subclass of type int with a name attribute"""
__slots__ = "_name" # also prevents additional attributes from being added
def __setattr__(self, name, value):
if hasattr(self, name):
raise AttributeError(
"'NamedInt' object attribute %r is read-only" % name)
else:
raise AttributeError(
"Cannot add attribute %r to 'NamedInt' object" % name)
def __new__(cls, name, value):
self = super(NamedInt, NamedInt).__new__(cls, value)
# avoid call to this subclass's __setattr__
super(NamedInt, self).__setattr__('_name', name)
return self
def __str__(self): # override string conversion to be name
return self._name
__repr__ = __str__
class NamedLong(long):
"""Subclass of type long with a name attribute"""
# note: subtypes of variable length 'long' type can't have __slots__
def __setattr__(self, name, value):
if hasattr(self, name):
raise AttributeError(
"NamedLong object attribute %r is read-only" % name)
else:
raise AttributeError(
"Cannot add attribute %r to 'NamedLong' object" % name)
def __new__(cls, name, value):
self = super(NamedLong, NamedLong).__new__(cls, value)
# avoid call to subclass's __setattr__
super(NamedLong, self).__setattr__('_name', name)
return self
def __str__(self):
return self._name # override string conversion to be name
__repr__ = __str__
class NamedWholeNumber(object):
"""Factory class which creates either a NamedInt or NamedLong
instance depending on magnitude of its numeric value.
Basically does the same thing as the built-in int() function
does but also assigns a '_name' attribute to the numeric value"""
class __metaclass__(type):
"""NamedWholeNumber metaclass to allocate and initialize the
appropriate immutable numeric type."""
def __call__(cls, name, value, base=None):
"""Construct appropriate Named* subclass."""
# note the int() call may return a long (it will also convert
# values given in a string along with optional base argument)
number = int(value) if base is None else int(value, base)
# determine the type of named numeric subclass to use
if -sys.maxint-1 <= number <= sys.maxint:
named_number_class = NamedInt
else:
named_number_class = NamedLong
# return instance of proper named number class
return named_number_class(name, number)
अच्छा जवाब, लेकिन 'नामांकित' ('हेक्सबेस', 'डेडबीफ', 16) को संभाल नहीं करता है। – martineau
हम्म, अच्छा बिंदु। मुझे लगता है कि इसे varargs के साथ तय किया जा सकता है। मैं ऐसा करने के लिए संपादित करूँगा। – Blckknght
फिक्स के लिए धन्यवाद। इस और @ एरिक्सन के जवाब के बीच फैसला करना मुश्किल था, क्योंकि दोनों डीआरवाई मुद्दे को बहुत अच्छी तरह से संबोधित करते थे - लेकिन आखिरकार इसे चुना क्योंकि यह सबसे सरल और समझने योग्य आईएमएचओ है। बीटीडब्लू, ''__slots__' विशेषता को केवल' नामांकित 'उप-वर्ग (जैसे एरिक्सन ने किया) में जोड़ना संभव है, जो उस पते को संबोधित करता है जिसकी संभावना अधिक सामान्य' int' केस (और _is_ उपयोग के लिए एक महत्वपूर्ण विशेषता है) की आवश्यकता है। – martineau