2 Replies - 718 Views - Last Post: 23 August 2011 - 05:44 AM Rate Topic: -----

#1 chemicalfan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 95
  • Joined: 16-October 09

Class inheritance, with instances

Posted 23 August 2011 - 02:24 AM

Sorry to keep spamming the board up with questions, it's just the latest project I'm doing is the most ambitious I've attempted, and I want to code it in the best way possible (the Pythonic way!). Anyway, hopefully someone will find it useful in years to come when Googling *shrug*

Basically, I know about class inheritance, and that it has it's uses. I think that it would be beneficial in this part of my program, but only if it can be implemented at 'instance level'. This might be hard to explain, so bear with me.

I'm looking at a "meeting" class, which will have certain attributes, values of which are pulled from another class (e.g. room). For each "type of meeting", there will be different attributes (e.g. attendees, agenda). Now, I was thinking that the "type of meeting" should be a subclass, so it can access info about the room (stuff like equipment, etc)? Here's the catch - there will be different instances of "meeting" for each day. Now, am I better off just having the one instance of "meeting", and having a method to update the attributes each day? Or will the "type of meeting" instances have access to the 'dynamic' attributes of the "meeting" class somehow?

Or is inheritance the wrong way to go??

Some pseudocode:

class Meeting(object):
def __init__(self,roomequip,roomloc):
self.equipment = roomequip
self.location = roomloc

class MeetingType(Meeting):
def __init__(self,name):
self.typename = name

def roomcheck(self):
print Meeting.location


Now, that "roomcheck" method might be better off in the Meeting class, then I could call it using 'super'? Anyway, basically there will be a Meeting class instatiated before the MeetingType, but the Meeting instance will be destroyed and another one created with different attributes. I can't see a way of automating this, without bundling lots of code into the __init__ methods to update attributes of existing instances. And if I'm doing that, it makes inheritance an unnecessary complication...

Is This A Good Question/Topic? 0
  • +

Replies To: Class inheritance, with instances

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7295
  • View blog
  • Posts: 15,185
  • Joined: 16-October 07

Re: Class inheritance, with instances

Posted 23 August 2011 - 04:26 AM

class Meeting(object):
	def __init__(self,roomequip,roomloc):
		self.equipment = roomequip
		self.location = roomloc

# this may be overkill if it lends nothing
class MeetingType(Meeting):
	def __init__(self,name):
		# is extend Meeting
		# but we don't initialize it's two members
		# this is wrong
		self.typename = name

def roomcheck(self):
	# Meeting is the name of a class
	# how does this work?
	print Meeting.location



Python's class based OO is to make your life easier and more formal. You don't actually need it, you can throw around dictionaries, lists, sets, tuples, etc, to get the same functionality.

Why not do the program without a class at all, then see where it will improve your flow? You can reasonably add things to a Meeting instance after the fact, which is a very Pythony thing to do.

class Meeting(object):
	def __init__(self,roomequip,roomloc):
		self.equipment = roomequip
		self.location = roomloc

meeting1 = Meeting(None, "Main Room")
meeting1.equipment = ('Projector','Laptop','Outlet')
meeting1.meetingType = 'Presentation'


Was This Post Helpful? 1
  • +
  • -

#3 chemicalfan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 95
  • Joined: 16-October 09

Re: Class inheritance, with instances

Posted 23 August 2011 - 05:44 AM

I'm using getter's & setter's to interact with object attributes, to try to simplfy the "main" part of the program (I'd rather do things as function calls, as I'm free to be dynamic, thanks to the getattr stuff in one of my other threads!)

The example code was just that, an example. The real code is way more complex, and I have to use classes to keep tabs on it. Back-in-the-day, I used BASIC where OO wasn't available, and I just had to bury the code in REM statements and GOSUBs to get some sense of what was going on. The "MeetingType" part wasn't well described - in the real version it where the magic really happens.

I take what you say onboard though, and I think that using methods to keep the attributes updated isn't the worst idea. Inheritance is something I've never used in programming (I knew I did the init wrong though, it was psuedocode after all ;)), it appears that I still can't find a use for it for the way I'm working. Who knows, maybe I'll need it later, but it always seems to be that there's a better way to do it. "The Zen of Python" just doesn't seem to apply, c'est la vie I guess

This post has been edited by chemicalfan: 23 August 2011 - 05:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1