python传递变量到类的方法中_类之间的Python传递变量

I'm trying to create a character generation wizard for a game. In one class I calculate the attributes of the character. In a different class, I'm displaying to the user which specialties are available based on the attributes of the character. However, I can't remember how to pass variables between different classes.

Here is an example of what I have:

class BasicInfoPage(wx.wizard.WizardPageSimple):

def __init__(self, parent, title):

wiz.WizardPageSimple.__init__(self, parent)

self.next = self.prev = None

self.sizer = makePageTitle(self, title)

self.intelligence = self.genAttribs()

class MOS(wx.wizard.WizardPageSimple):

def __init__(self, parent, title):

wiz.WizardPageSimple.__init__(self, parent)

self.next = self.prev = None

self.sizer = makePageTitle(self, title)

def eligibleMOS(self, event):

if self.intelligence >= 12:

self.MOS_list.append("Analyst")

The problem is that I can't figure out how to use the "intelligence" variable from the BasicInfoPage class to the MOS class. I've tried several different things from around the Internet but nothing seems to work. What am I missing?

Edit I realized after I posted this that I didn't explain it that well. I'm trying to create a computer version of the Twilight 2000 RPG from the 1980s.

I'm using wxPython to create a wizard; the parent class of my classes is the Wizard from wxPython. That wizard will walk a user through the creation of a character, so the Basic Information page (class BasicInfoPage) lets the user give the character's name and "roll" for the character's attributes. That's where the "self.intelligence" comes from.

I'm trying to use the attributes created her for a page further on in the wizard, where the user selects the speciality of the character. The specialities that are available depend on the attributes the character has, e.g. if the intelligence is high enough, the character can be an Intel Anaylst.

It's been several years since I've programmed, especially with OOP ideas. That's why I'm confused on how to create what's essentially a global variable with classes and methods.

解决方案

You may have "Class" and "Instance" confused. It's not clear from your example, so I'll presume that you're using a lot of class definitions and don't have appropriate object instances of those classes.

Classes don't really have usable attribute values. A class is just a common set of definitions for a collection of objects. You should think of of classes as definitions, not actual things.

Instances of classes, "objects", are actual things that have actual attribute values and execute method functions.

You don't pass variables among classes. You pass variables among instances. As a practical matter only instance variables matter. [Yes, there are class variables, but they're a fairly specialized and often confusing thing, best avoided.]

When you create an object (an instance of a class)

b= BasicInfoPage(...)

Then b.intelligence is the value of intelligence for the b instance of BasicInfoPage.

A really common thing is

class MOS( wx.wizard.PageSimple ):

def __init__( self, parent, title, basicInfoPage ):

self.basicInfo= basicInfoPage

Now, within MOS methods, you can say self.basicInfo.intelligence because MOS has an object that's a BasicInfoPage available to it.

When you build MOS, you provide it with the instance of BasicInfoPage that it's supposed to use.

someBasicInfoPage= BasicInfoPage( ... )

m= MOS( ..., someBasicInfoPage )

Now, the object m can examine someBasicInfoPage.intelligence

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将方法变量保存到类中变量,你可以使用实例属性属性。 1. 使用实例属性保存方法变量: ```python class MyClass: def __init__(self): self.data = None # 创建一个实例属性 def set_data(self, value): self.data = value # 将方法变量保存到实例属性 def get_data(self): return self.data # 获取保存的数据 obj = MyClass() obj.set_data(10) print(obj.get_data()) # 输出:10 ``` 在上述示例,我们在 `__init__` 方法创建了一个名为 `data` 的实例属性。然后,通过 `set_data` 方法,我们将方法变量保存到该实例属性,并通过 `get_data` 方法获取保存的数据。 2. 使用属性保存方法变量: ```python class MyClass: data = None # 创建一个属性 @staticmethod def set_data(value): MyClass.data = value # 将方法变量保存到属性 @staticmethod def get_data(): return MyClass.data # 获取保存的数据 MyClass.set_data(20) print(MyClass.get_data()) # 输出:20 ``` 在上述示例,我们直接在类中创建了一个名为 `data` 的属性。通过使用 `@staticmethod` 装饰器,我们定义了 `set_data` 和 `get_data` 方法。在 `set_data` 方法,我们将方法变量保存到属性 `data` ,并通过 `get_data` 方法获取保存的数据。 无论是使用实例属性还是属性,你都可以在方法保存变量,并在需要时访问它们。选择使用哪种方式取决于你的需求和设计。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值