在类的多种方法中使用self [英] The use of self in multiple methods of a class
问题描述
因此,基本上,作为一个初学者程序员,我创建了一个程序,该程序使用Tkinter计算口袋妖怪游戏中不同口袋妖怪/糖果组合的最大可能进化。我只是在将函数与代码一起使用之前创建了此程序。但是,我认为最好使用一堂课,经过一番研究,这就是结果。但是,我基本上是通过在不同的方法中向一堆变量中添加self来使其工作的。
So basically as a beginner programmer I created this program that calculates maximum possible evolves for different pokemon/candy combinations in pokemon go using Tkinter. I created this program before just using a function with al the code. However, I thought it would be good to use a class and after some research this was the result. However, I basically got it working by adding self to a whole bunch of variables in the different methods.
我的问题是:类的使用在这里是否还有意义?我对类的研究涉及用对象等填充类,但在这里我只是创建1个对象以使用该类并运行大量计算。
My question is: Is the use of a class even relevant here. My research on classes involved populating a class with objects etc, but here i just create 1 object in order to use the class and run a bunch of calculations.
此外,如何知道在调用变量时何时使用self。例如,我创建静态类变量时会觉得我不必将实例self放在实例的前面就可以使用它,但是那没有用,所以我只是在变量调用的前面放了更多selfs。为什么将一堆自我摆在所有事物面前会起作用?自我过度杀伤了吗?我可以用更聪明的方式吗?
Furthermore, how do I know when to use self when calling a variable. For instance I create static class variables with the impression that i don't have to put the instance self in front of it to use it, but that didn't work so I just put more selfs in front of the calling of the variable. Why does it work to put a bunch of selfs in front of everything? Are the selfs overkill and could I do it in some smarter way?
from tkinter import *
from tkinter import messagebox
class PokeCalculator(Frame):
pokedex = {
'pidgey': 12,
'caterpie': 12,
'weedle': 12,
'rattata': 25
}
choices = pokedex.keys()
screen_title = 'Pokemon evolve calculator'
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.make_window()
def make_window(self):
self.master.title(self.screen_title)
L1 = Label(self, text='Candies')
L1.grid(column=1, row=0)
L2 = Label(self, text='Pokemon amount in storage')
L2.grid(column=2, row=0)
self.var = StringVar()
self.var.set('Pokemon')
self.Pokemon = OptionMenu(self, self.var, *self.choices)
self.Pokemon.grid(column=0, row=1)
self.Candies = Entry(self)
self.Candies.grid(column=1, row=1)
self.Poke_amount = Entry(self)
self.Poke_amount.grid(column=2, row=1)
Calculate = Button(self, text='Calculate', command=self.get_and_check)
Calculate.grid(column=1, row=2)
def get_and_check(self):
self.get_values()
self.check_input()
def get_values(self):
self.poke = self.var.get()
self.candies_total = self.Candies.get()
self.p_amount = self.Poke_amount.get()
def check_input(self):
string1 = 'Please select a Pokemon from the dropdown menu'
string2 = 'Please only enter numbers'
if self.poke == 'Pokemon':
messagebox.showinfo(self.screen_title, string1)
elif not self.p_amount.isdigit() or not self.candies_total.isdigit():
messagebox.showinfo(self.screen_title, string2)
else:
self.too_few_pokemon()
def too_few_pokemon(self):
candies_total = int(self.candies_total)
p_amount = int(self.p_amount)
evolve = int((candies_total - 1) / (self.pokedex[self.poke] - 1))
candies_needed = (p_amount * (self.pokedex[self.poke] - 1)) + 1
if p_amount <= evolve:
n = 0
while candies_needed <= candies_total:
n = n + 1
p_amount = p_amount + 1
candies_needed = ((p_amount) * (self.pokedex[self.poke] - 1)) + 1
candies_total = candies_total + 3
evolve2 = int((candies_total - 1) / (self.pokedex[self.poke] - 1))
string1 = ''' You have enough candies too evolve {0} {1},
but you only have {2} {1} in storage and thus can only
evolve {2} {1}.
If you catch {3} more {1} you can now evolve {4} {1}.'''
messagebox.showinfo(self.screen_title, string1.format(evolve, self.poke,
self.p_amount,
n, evolve2))
else:
self.too_much_pokemon()
def too_much_pokemon(self):
candies_total = int(self.candies_total)
p_amount = int(self.p_amount)
candies_needed = (p_amount * (self.pokedex[self.poke] - 1)) + 1
m = 0
while candies_total <= candies_needed:
m = m + 1
p_amount = p_amount - 1
candies_needed = ((p_amount) * (self.pokedex[self.poke] - 1)) + 1
candies_total = candies_total + 1
evolve = int((candies_total - 1) / (self.pokedex[self.poke] - 1))
string2 = 'Transfer {0} {1} so you can evolve a total of {2} {1}.'
messagebox.showinfo(self.screen_title, string2.format(m, self.poke, evolve))
root = Tk()
app = PokeCalculator(root)
app.pack()
root.mainloop()
推荐答案
self
引用实例(对象)。在python中的类中,属性有2种类型。类属性和实例属性。
self
refers to the instance (object) of that class. There are 2 types of attributes in a class in python. Class attributes and instance attributes.
https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables
实例属性是特定于类的实例(对象)。
类属性是相对于类的。这些属性由类的所有实例共享。
Instance attributes are specific to the instance (object) of a class.
Class attributes are with respect to a class. These attributes are shared by all instances of a class.
在您的代码中, pokedex
,选择
和 screen_title
都是类属性的示例。诸如 self.master
, self.var
, self.Pokemon $ c $之类的属性c>是实例属性的示例。
In your code, pokedex
, choices
and screen_title
are all examples of class attributes. Attributes like self.master
, self.var
, self.Pokemon
are examples of instance attributes.
在这里使用的类是否相关?
我会说是。类是数据和方法的逻辑分组。它封装了一些数据以及可以对该数据执行的一些方法。我们不仅仅是在一个类中将随机的东西扔在一起,而是尝试创建在事物之间存在逻辑联系的类。
I would say YES. A class is a logical grouping of data and methods. It encapsulates some data and a few methods that can be performed on that data. Rather than just throwing random things together in a class, we try to create classes where there is a logical connection between things.
这可能是一个有用的来源:
https: //jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
This might be an useful source: https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
这篇关于在类的多种方法中使用self的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!