properties - How to define a default list of attributes for a Python object? -
i have few objects in program have 10-15 attributes each. these attributes set @ various places in code in various stages in program. absolutely fine set them on objects without having them defined in class in python, there's no authoritative list in program of attributes used.
i'd following:
class myobject(object): attr1 attr2
i've looked @ property()
built-in, it's @ least 6 lines of code per property. there easy way define list of default attributes without initializing them in python?
update: forgot mention, of values don't have sensible defaults , equal none
before used.
like this?
class myobject(object): def __init__(self, attr1 = default1, attr2 = default2): self.attr1 = attr1 self.attr2 = attr2
you can instantiate myobject
or without specifying attributes
myobject1 = myobject() # gets default values myobject2 = myobject(foo, bar) # overrides defaults
you use keyword arguments (kwargs
) if have variable number of attributes, see here examples.
Comments
Post a Comment