简体   繁体   English

python:0x的类错误__main__.foo实例

[英]python: Class error __main__.foo instance at 0x

I'm learning python (with VBA background) by building a black-jack game (Yes, I've asked a bunch of questions using blackjack as an example) . 我正在通过构建一个黑杰克游戏来学习python(具有VBA背景) (是的,我已经使用二十一点问了一堆问题)

Here's my code: 这是我的代码:

import random

class DECK():
    def load_deck(self):
        suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
        rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
        full_deck = {}
        i = 0
        for s in suite:
            for r in rank:
                full_deck[i] = "%s of %s" % (r, s)
                i += 1
        return full_deck

    def pick_item(self, deck):   
        card_key = random.choice(deck.keys())  
        new_card = deck[card_key] 
        del deck[card_key]  
        return (deck, new_card)

    def missing_card(self, deck):
        temp_deck = DECK()
        print temp_deck

d1 = DECK()

deck1 = d1.load_deck()

deck1, card1 = d1.pick_item(deck1)

print card1

d1.missing_card(d1)

Here's what I get in the terminal (file name hand_c.py ) : 这是我在终端中得到的(文件名hand_c.py

$ python hand_c.py
Ace of Clubs
<__main__.DECK instance at 0x10bb0d248>
$ 

Why does one function work pick_item , but not the other missing_card ? 为什么一个函数工作pick_item ,而不是另一个missing_card

Per the first answer, I changed the function definition to: 根据第一个答案,我将函数定义更改为:

    def missing_card(self, deck):
    deckC1 = DECK()
    temp_deck = deckC1.load_deck
    print temp_deck

But now I get the following from the terminal: 但现在我从终端获得以下内容:

$ python hand_c.py
Jack of Diamonds
<bound method DECK.load_deck of <__main__.DECK instance at 0x10500e248>>
$ 

I modified your program to work. 我修改了你的程序。 I introduced a constructor and changed the representation of your deck to a list instead of a dictionary, and made it to a instance variable that belongs to the deck you create in the line d1 = DECK() . 我介绍了一个构造函数,并将您的deck的表示形式更改为列表而不是字典,并将其更改为属于您在行d1 = DECK()创建的卡片组的实例变量。 Every method in your class now has access to your deck, without revealing your internal representation of the deck to the world and you only have to work with the one DECK object. 您班上的每个方法现在都可以访问您的套牌,而不会向世界透露您的套牌的内部表示,您只需要使用一个DECK对象。

import random

class DECK():
    def __init__(self):
        suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
        rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
        self.full_deck = []
        for s in suite:
            for r in rank:
                self.full_deck.append("%s of %s" % (r, s))

    def pick_item(self):
        card_key = random.randint(0, len(self.full_deck)-1)  
        new_card = self.full_deck[card_key] 
        del self.full_deck[card_key]  
        return new_card

    def missing_card(self):
        print self.full_deck

d1 = DECK()
card1 = d1.pick_item()

print card1

d1.missing_card()

You should go through the Python tutorial . 你应该通过Python教程 Your code has many problems. 你的代码有很多问题。 Most basically, you are using the class just as a bag to hold functions, without actually holding the deck data as part of the class instance. 最基本的是,你正在使用类作为一个包来保存函数,而不是实际上将deck数据作为类实例的一部分。 That is, you return the deck as a dictionary, then pass it back in to another function. 也就是说,您将套牌作为字典返回,然后将其传回另一个函数。 It would be better to store the deck in an attribute (eg, self.deck ) and then have other functions use that. 最好将deck存储在属性(例如self.deck )中,然后让其他函数使用它。

Anyway, the reason it doesn't print the dictionary is quite simple. 无论如何,它不打印字典的原因很简单。 You do this: 你做这个:

    temp_deck = DECK()
    print temp_deck

So you create a variable temp_deck and set it equal to a new instance of class DECK. 因此,您创建一个变量temp_deck并将其设置为等于DECK类的新实例。 Then you print it. 然后你打印它。 Well, of course it won't print a dictionary. 好吧,当然它不会打印字典。 temp_deck is not a dictionary. temp_deck不是字典。 It's a DECK object. 这是一个DECK对象。 If you want the dictionary with your current code, you would need to do temp_deck.load_deck() just like you did with your original deck, and then print the result of that. 如果你想要使用当前代码的字典,你需要像使用原始套牌一样进行temp_deck.load_deck() ,然后打印出结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM