Re: Bring object 'out of' Class?

2008-06-01 Thread Arnaud Delobelle
dave [EMAIL PROTECTED] writes:

 Hello,

 I'm currently on the class section of my self-taught journey and have
 a question about classes:  is it possible to bring a object created
 inside the class definitions outside the class so it can be accessed
 in the interpreter?

 For example, right now I'm working (within Allen Downey's Python
 Programmer book) with creating a 'hand' of cards.  I want to be able
 to deal to 'x' amount of cards to 'x' amount of hands and then be able
 to manipulate those hands afterwards.  I'm not sure if even what I'm
 asking is possible or if I'm getting ahead of myself.

 As always, thanks for all your help.  My learning is greatly enhanced
 with everyone's input on this board.  Please feel free to
 comment/critique the code...

 Here is the section of code that deals hands (but doesn't do anything
 past that):

def deal_cards(self, num_of_hands, num):
'''deals x amount of cards(num) to each hand'''
for i in range(num_of_hands):
handname = Hand('hand%d' % i)
self.deal(handname, num)
print '%s' % (handname.label), '\n', handname, '\n'


You need to use a 'return' statement:

   def deal_cards(self, num_of_hands, num):
   '''deals x amount of cards(num) to each hand'''
   hands = []
   for i in range(num_of_hands):
   newhand = Hand('hand%d' % i)
   self.deal(newhand, num)
   hands.append(newhand)
   print '%s' % (handname.label), '\n', handname, '\n'
   return Hand


Then you can write:

 hands = deck.deal_cards(4, 5) # On fait une belotte?

And I don't see the need of defining 'Hand' inside 'Deck'.

HTH

-- 
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bring object 'out of' Class?

2008-06-01 Thread member thudfoo
On 6/1/08, Arnaud Delobelle [EMAIL PROTECTED] wrote:
 dave [EMAIL PROTECTED] writes:

[..]

  
  def deal_cards(self, num_of_hands, num):
  '''deals x amount of cards(num) to each hand'''
  for i in range(num_of_hands):
  handname = Hand('hand%d' % i)
  self.deal(handname, num)
  print '%s' % (handname.label), '\n', handname, '\n'
  


 You need to use a 'return' statement:


def deal_cards(self, num_of_hands, num):
'''deals x amount of cards(num) to each hand'''

hands = []

for i in range(num_of_hands):

newhand = Hand('hand%d' % i)
self.deal(newhand, num)
hands.append(newhand)

print '%s' % (handname.label), '\n', handname, '\n'

return Hand

Should be: return hands



  Then you can write:

   hands = deck.deal_cards(4, 5) # On fait une belotte?

[...]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bring object 'out of' Class?

2008-06-01 Thread dave

Then you can write:


hands = deck.deal_cards(4, 5) # On fait une belotte?


And I don't see the need of defining 'Hand' inside 'Deck'.

HTH


Thanks for the input.

I believe using 'class Hand(Deck):' is to illustrate (in the book) 
inheritance and how it can be used.  By using 'Hand(Deck)' I can then 
use the methods (pop_card, add_cards, etc..) defined in the 'Deck' 
class.



--
http://mail.python.org/mailman/listinfo/python-list


Bring object 'out of' Class?

2008-05-31 Thread dave

Hello,

I'm currently on the class section of my self-taught journey and have a 
question about classes:  is it possible to bring a object created 
inside the class definitions outside the class so it can be accessed in 
the interpreter?


For example, right now I'm working (within Allen Downey's Python 
Programmer book) with creating a 'hand' of cards.  I want to be able to 
deal to 'x' amount of cards to 'x' amount of hands and then be able to 
manipulate those hands afterwards.  I'm not sure if even what I'm 
asking is possible or if I'm getting ahead of myself.


As always, thanks for all your help.  My learning is greatly enhanced 
with everyone's input on this board.  Please feel free to 
comment/critique the code...


Here is the section of code that deals hands (but doesn't do anything 
past that):


   def deal_cards(self, num_of_hands, num):
   '''deals x amount of cards(num) to each hand'''
   for i in range(num_of_hands):
   handname = Hand('hand%d' % i)
   self.deal(handname, num)
   print '%s' % (handname.label), '\n', handname, '\n'



and here is the all of the code:

#!/usr/bin/env python

import random

class Card:
   represents a playing card
   attributes: rank, suit

   def __init__(self, suit=0, rank=3):
   self.suit = suit
   self.rank = rank

   suit_names = [Clubs, Diamonds, Hearts, Spades]
   rank_names = [None, Ace, Two, Three, Four, Five, Six, Seven,
 Eight, Nine, Ten, Jack, Queen, King]

   def __str__(self):
   #prints card in format: Rank 'of' Suit#
   return '%s of %s' % (Card.rank_names[self.rank],
   Card.suit_names[self.suit])

   def __cmp__(self, other):
   #evaluates which card is ranked higher by suit/rank
   #arbitrary definition. depends on the card game
   card1 = self.suit, self.rank
   card2 = other.suit, other.rank
   return cmp(card1, card2)


class Deck:
   represents a deck of cards. 52 card, 4 suits, 13 cards/suit

   def __init__(self):
   self.cards = []
   for suit in range(4):
   for rank in range(1, 14):
   card = Card(suit, rank)
   self.cards.append(card)

   def __str__(self):
   res = []
   for card in self.cards:
   res.append(str(card))
   return '\n'.join(res)

   def pop_card(self):
   '''removes a card and returns it to another object'''
   return self.cards.pop()

   def add_cards(self, card):
   #adds a card to an object
   return self.cards.append(card)

   def shuffle(self):
   '''must import random, shuffles the deck'''
   random.shuffle(self.cards)

   def sort_deck(self):
   self.cards.sort()

   def deal(self, hand, num):
   '''moves cards from one object to another'''
   for i in range(num):
   hand.add_cards(self.pop_card())

   def deal_cards(self, num_of_hands, num):
   '''deals x amount of cards(num) to each hand'''
   for i in range(num_of_hands):
   handname = Hand('hand%d' % i)
   self.deal(handname, num)
   print '%s' % (handname.label), '\n', handname, '\n'




   class Hand(Deck):
   child class of Deck. Represents a hand of cards

   def __init__(self, label= ):
   self.cards = []
   self.label = label

--
http://mail.python.org/mailman/listinfo/python-list