5 Replies - 297 Views - Last Post: 03 February 2014 - 06:59 AM Rate Topic: -----

#1 apathybear7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-June 13

Better way to connect classes?

Posted 02 February 2014 - 01:35 PM

Hey im new to python. I have been working on recreating a really stupid game, but im questioning some parts of my code. Specifically the part where i attempt to connect the engine class with all the other classes. Here is the full code.

class Girl(object):

	def unlock(self):
		print " You did it bro, you finally unlocked her heart. Wow, "," You have mad game!"
	def pissed_her_off(self):
		print "You messed up, you maybe should just give up"

class punk_chick(Girl):

	def begin(self):
		print "You walk straight up to punker chick"," and say something"
		raw_input("> ")

class edm_chick(Girl):

	def begin(self):
		print "You walk straight up to edm chick"," and say something"
		raw_input("> ")

class hipster_chick(Girl):

	def begin(self):
		print "You walk straight up to hipster chick"," and say something"
		raw_input("> ")

class preppy_chick(Girl):

	def begin(self):
		print "You walk straight up to preppy chick"," and say something"
		raw_input("> ")


#engine
class Engine(object):
	
	def __init__ (self):
		self.edm_chick = edm_chick()
		self.hipster_chick = hipster_chick()
		self.punk_chick = punk_chick()
		self.preppy_chick=preppy_chick()

	def start(self):
		


		print "yo pick a target: 1.edm 2.punker 3.hipster 4.preppy"
		choice = raw_input("> ")
		if choice == "1":
			self.edm_chick.begin()
		elif choice == "2":
			self.punk_chick.begin()
		elif choice == "3":
			self.hipster_chick.begin()
		elif choice == "4":
			self.preppy_chick.begin()
		else:
			print "not a valid choice, bro"


	def game_over(self):
		pass


player = Engine()
player.start()




This is the part that seems kind of redundant:

class Engine(object):
	
	def __init__ (self):
		self.edm_chick = edm_chick()
		self.hipster_chick = hipster_chick()
		self.punk_chick = punk_chick()
		self.preppy_chick=preppy_chick()



Is there a better way to do this? This just seems like a very tedious amount of code to get engine to talk to all the other classes.

PS: I thought about maybe making "engine" a child of those classes, but that would also seem messy.

Any advice is much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Better way to connect classes?

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Better way to connect classes?

Posted 02 February 2014 - 05:15 PM

How do you want to connect them. What is so redundant. It seems like you are just creating a class who is composed of instances of edm_chick, hipster_chick, punk_chick, and preppy_chick. There isn't much redundancy. Maybe you are trying to do it some other way.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Better way to connect classes?

Posted 02 February 2014 - 05:49 PM

Just for fun:
class Girl(object):
    def __init__(self, name):
        self.name = name

    def unlock(self):
        print " You did it bro, you finally unlocked her heart. Wow, "," You have mad game!"
    def pissed_her_off(self):
        print "You messed up, you maybe should just give up"

    def begin(self):
        print "You walk straight up to", self.name, "chick and say something"
        raw_input("> ")

# if you really need another class, which you currently don't
class punk_chick(Girl):
    def __init__(self):
        Girl.__init__(self, 'punker')


class Engine(object):
    def __init__ (self):
        self.chicks = [ Girl("edm"), punk_chick(), Girl("hipster"), Girl("preppy") ]

    def start(self):
        print "yo pick a target:",
        print ' '.join("%d. %s" % (i+1, x.name) for (i,x) in enumerate(self.chicks))

        choice = int(raw_input("> ")) - 1
        if choice>=0 and choice<len(self.chicks):
            self.chicks[choice].begin()
        else:
            print "not a valid choice, bro"


    def game_over(self):
        pass



Hope this helps.

This post has been edited by baavgai: 02 February 2014 - 05:49 PM

Was This Post Helpful? 1
  • +
  • -

#4 apathybear7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-June 13

Re: Better way to connect classes?

Posted 03 February 2014 - 02:38 AM

Thanks baavgai. That was pretty helpful in showing me another way to do things. Your code is much cleaner (mostly).

while line 26
print ' '.join("%d. %s" % (i+1, x.name) for (i,x) in enumerate(self.chicks))

seems hard for no reason, I am interested in understanding what is going on. Can you explain this further? I know this is a simple for-in loop, but it seems to have a lot going on.

Seriously thanks!
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: Better way to connect classes?

Posted 03 February 2014 - 04:03 AM

Heh. Well, I guess it's hard for a reason?

Ok, you have a list of things and you want to print them. Let's walk our way to it.
>>> a = ['Alpha','Bravo','Charlie']
>>> a
['Alpha', 'Bravo', 'Charlie']
>>> # print the list
...
>>> for x in a:
...     print x
...
Alpha
Bravo
Charlie
>>> # I want numbers
...
>>> for x in enumerate(a):
...     print x
...
(0, 'Alpha')
(1, 'Bravo')
(2, 'Charlie')
>>> # I want number + 1 in the format of 1. Alpha...
...
>>> for (i,x) in enumerate(a):
...     print "%d. %s" % ((i+1),x)
...
1. Alpha
2. Bravo
3. Charlie
>>> # I want this as a list of strings
...
>>> a2 = []
>>> for (i,x) in enumerate(a):
...     a2.append("%d. %s" % ((i+1),x))
...
>>> a2
['1. Alpha', '2. Bravo', '3. Charlie']
>>> # I want to create that list of strings in a single line
...
>>> a3 = [ "%d. %s" % ((i+1),x) for (i,x) in enumerate(a) ]
>>> a3
['1. Alpha', '2. Bravo', '3. Charlie']
>>> # I want that as a single string
...
>>> ' '.join(a3)
'1. Alpha 2. Bravo 3. Charlie'
>>> # putting it all together
...
>>> print ' '.join("%d. %s" % ((i+1),x) for (i,x) in enumerate(a))
1. Alpha 2. Bravo 3. Charlie
>>>



Contrary to Python propaganda, there are clearly a whole lot of ways I could have gotten that final result. However, being able to use a single line with a list comprehension was the most satisfying way to write it. If you've never seen it before, it's admittedly a little hard to worry out. If you play with Python enough, though, these kind of things should become second nature.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Better way to connect classes?

Posted 03 February 2014 - 06:59 AM

There is an introduction to list comprehensions here.

DIC doesn't have one specifically on list (and dictionary) comprehensions. Perhaps we should ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1