coding style - Factory method for python object - best practice -
this question regarding best practice creating instance of class or type different forms of same info using python. improve utilize class method or improve utilize separate function altogether? lets have class used describe size of document. (note: example. want know best way create instance of class not best way describe size of document.)
class size(object): """ utility object used describe size of document. """ byte = 8 kilo = 1024 def __init__(self, bits): self._bits = bits @property def bits(self): homecoming float(self._bits) @property def bytes(self): homecoming self.bits / self.byte @property def kilobits(self): homecoming self.bits / self.kilo @property def kilobytes(self): homecoming self.bytes / self.kilo @property def megabits(self): homecoming self.kilobits / self.kilo @property def megabytes(self): homecoming self.kilobytes / self.kilo
my __init__
method takes size value represented in bits (bits , bits , want maintain way) lets have size value in bytes , want create instance of class. improve utilize class method or improve utilize separate function altogether?
class size(object): """ utility object used describe size of document. """ byte = 8 kilo = 1024 @classmethod def from_bytes(cls, bytes): bits = bytes * cls.byte homecoming cls(bits)
or
def create_instance_from_bytes(bytes): bits = bytes * size.byte homecoming size(bits)
this may not seem issue , perhaps both examples valid think every time need implement this. long time have preferred class method approach because organisational benefits of tying class , mill method together. also, using class method preserves ability create instances of subclasses it's more object orientated. on other hand, friend 1 time said "when in doubt, standard library does" , yet find illustration of in standard library.
any feedback much appreciated.
cheers
first, of time think need this, don't; it's sign you're trying treat python java, , solution step , inquire why need factory.
often, simplest thing have constructor defaulted/optional/keyword arguments. cases you'd never write way in java—even cases overloaded constructors sense wrong in c++ or objc—may natural in python. example, size = size(bytes=20)
, or size = size(20, size.bytes)
reasonable. matter, bytes(20)
class inherits size
, adds absolutely nil __init__
overload looks reasonable. , these trivial define:
def __init__(self, *, bits=none, bytes=none, kilobits=none, kilobytes=none):
or:
bits, bytes, kilobits, kilobytes = 1, 8, 1024, 8192 # or object(), object(), object(), object() def __init__(self, count, unit=size.bits):
but, do need mill functions. so, do then? well, there 2 kinds of things lumped "factories".
a @classmethod
idiomatic way "alternate constructor"—there examples on stdlib—itertools.chain.from_iterable
, datetime.datetime.fromordinal
, etc.
a function idiomatic way "i don't care actual class is" factory. at, e.g., built-in open
function. know returns in 3.3? care? nope. that's why it's function, not io.textiowrapper.open
or whatever.
your given illustration seems legitimate utilize case, , fits pretty "alternate constructor" bin (if doesn't fit "constructor arguments" bin).
python coding-style decorator class-method factory-method
No comments:
Post a Comment