There is few views you can have in OOP and Python. This is from a guy more familiar with structured/modular programming than object oriented.
Python lets you use objects when you need, otherwise do not bother. That means generally that when you have many instances you want to create, which become clearer by using classes. It means you do not write slow pure Python code to reinvent list or dict but use builtins or inherit from them. Generally in Python you use builtin dict a lot. You would also use OOP to avoid need of global variables.
Actually many things you do not think so are an object instance like your script module or function. So you can create instance variables for them.
If the operation would make sense assuming as little as possible of the type of parameters ('duck typed'), it make sense to use general function of the module instead of putting it in class.
You do not however use getter or setter methods in Python, if needed attribute is later easy to change as calculated property, but only if needed so.
The classic http://dirtsimple.org/2004/12/python-is-not-java.html
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
I still not get what you want to do, I only mean you would use
car.weight
not
car.get_weight()
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
There is many alternatives, for example, you coud define values as namedtuple allowing field access, or using dictionary of dictionaries.
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
get_width is so so. Here some experiments:
from collections import namedtuple
class Test(object):
def __init__(self, **mydict):
self.mydict = mydict
def __str__(self):
return '\n'.join('%s = %s' % v for v in self.mydict.items())
Info = namedtuple("Info", "height, width, length")
my_data = Test(tony = Info(183,23, 12), shoe = Info(175, 12, 45))
print my_data
#named tuple field can be accessed, but can not change
print my_data.mydict['shoe'].width
print
class Info(object):
__slots__ = 'height', 'width', 'length'
def __init__(self, height, width, length):
self.height, self.width, self.length = height, width, length
def __str__(self):
return 'Info(%s)' % ','.join('%s = %s' % (v, getattr(self, v)) for v in self.__slots__)
my_data = Test(tony = Info(183,23, 12), shoe = Info(175, 12, 45))
print my_data
print
#with Info class the field is writable
my_data.mydict['tony'].width = 44
print my_data
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Tweaking the field access and dropping mydict and inheriting from dict:
from collections import namedtuple
class Test(dict):
def __init__(self, **mydict):
self.update(mydict)
__getattr__ = dict.__getitem__
def __setattr__(self, key, value):
self.__dict__[key] = value
def __str__(self):
return '\n'.join('%s = %s' % v for v in self.items())
Info = namedtuple("Info", "height, width, length")
my_data = Test(tony = Info(183,23, 12), shoe = Info(175, 12, 45))
print my_data
#named tuple field can be accessed, but can not change
print my_data.shoe.width
print
class Info(object):
__slots__ = 'height', 'width', 'length'
def __init__(self, height, width, length):
self.height, self.width, self.length = height, width, length
def __str__(self):
return 'Info(%s)' % ','.join('%s = %s' % (v, getattr(self, v)) for v in self.__slots__)
my_data = Test(tony = Info(183,23, 12), shoe = Info(175, 12, 45))
print my_data
print
#with Info class the field is writable
my_data.tony.width = 44
print my_data
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Good that you found a suitable data structure for your use, and thank you for inspiring one of my entries to the code snippet competition!
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Couldn't you try same method as my typed dictionary Code Snippet?
pyTony
pyMod
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
Question Answered as of 9 Months Ago by
pyTony