python创建一个类的实例,python:在另一个类中创建类的实例(带有通用示例)

I'm learning python via book and internet and I'm stuck on a class issue.

2 questions:

How do I create an instance of one class in another (separate) class?

How do I pass variables between the class and the nested(?) class?

When I try to create an instance of a class within another (separate) class, i'm able to do so within a method. Here's the code:

import os

class FOO():

def __init__(self):

self.values = [2, 4, 6, 8]

def do_something(self, x, y):

os.system("clear")

z = self.values[x] * self.values[y]

print "%r * %r = %r" % (self.values[x], self.values[y], z)

class BAR():

def __init__(self):

pass

def something_else(self, a, b):

foo1 = FOO()

foo1.do_something(a, b)

bar = BAR()

bar.something_else(1, 2)

Will this, however, allow me to access the FOO class and it's information in other BAR methods? If so, how?

I tried the following and got an error:

import os

class FOO():

def __init__(self):

self.values = [2, 4, 6, 8]

def do_something(self, x, y):

os.system("clear")

z = self.values[x] * self.values[y]

print "%r * %r = %r" % (self.values[x], self.values[y], z)

class BAR():

def __init__(self):

foo1 = FOO()

def something_else(self, a, b):

foo1.do_something(a, b)

bar = BAR()

bar.something_else(1, 2)

Here is the error:

Traceback (most recent call last):

File "cwic.py", line 22, in

bar.something_else(1, 2)

File "cwic.py", line 19, in something_else

foo1.do_something(a, b)

NameError: global name 'foo1' is not defined

I get the same error when I used:

def __init__(self):

self.foo1 = FOO()

Also, how should I pass the 'values' from one class to the other?

Please let me know if my general approach and/or syntax are wrong. Any and all suggestions welcome (including good reading) as I am just learning. Thanks.

解决方案

All attributes of an instance or class are accessed via self which is passed as the first argument to all methods. That's why you've correctly got the method signature something_else(self, a, b) as opposed to just something_else(a, b) as you might with other languages. So you're looking for:

class BAR():

def __init__(self):

self.foo1 = FOO()

def something_else(self, a, b):

self.foo1.do_something(a, b)

Note that self isn't a keyword, it's just a paramater name like a and b, so you might for example be tempted to use this instead if you're from a Java background... Don't though! there is a very strong convention to use self for this purpose and you will make a lot of people (including yourself eventually) very angry if you use anything else!

With regards to passing values I'm not sure what you mean, you can access them through instances such as foo1.an_attribute or pass them as arguments to functions as in function(arg) but you seem to have used both techniques in your example so I'm not sure what else you're after...

Edit

In response to @dwstein's comment, I'm not sure how it's done in VB but here's a how you pass arguments when creating an instance of a class.

If we look at your class:

class BAR():

def __init__(self):

self.foo1 = FOO()

We can change that to accept an argument baz by changing second line to def __init__(self, baz): and then if we want we can make baz an attribute of our new instance of BAR by setting self.baz = baz. Hope that helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值