类中的全局变量。 [英] Global variables within classes.

查看:108
本文介绍了类中的全局变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我让自己真的很困惑。我有三节课。其中两个需要

来引用第三个,如下所示:


Canvas --- Stack< --- Thing

我现在正在这样做:


s = Stack()


然后在<类Canvas中:我是直接引用全局

变量'''''(又来自Thing)。


现在,这有效,但它让我有点烦恼。我不想限制用户

必须声明一个特定名称的全局变量。

他们应该能够说< Zombie = Stack( )>


这也可能导致更多的混乱,因为用户也可以直接制作物品

< mything = Thing()我想要新的所有事情都参考了

*相同* Stack。 (现在它起作用了,因为我在Thing中引用's''。


我认为这可能是多重继承的情况,但我只是保持

得到递归错误,因为有一件事是指另一件事在一个大混乱中。


这有意义吗?


\ d

解决方案

Donn Ingleaécrit:




我让自己很困惑。我有三节课。其中两个需要

来引用第三个,如下所示:


Canvas --- Stack< --- Thing

我现在正在这样做:


s = Stack()


然后在<类Canvas中:我是直接引用全局

变量'''''(又来自Thing)。


现在,这有效,但它让我有点烦恼。



确实......


我不想将用户限制为

必须声明一个特定名称的全局变量。

他们应该可以说< Zombie = Stack()>


这也可能导致更多的混乱,因为用户也可以直接制作物品< mything = Thing()并且我希望新的东西都能参考

*相同*堆栈。 (现在它起作用了,因为我在Thing中引用''s'。)


我认为这可能是多重继承的情况



???


但我只是保持

获取递归错误一件事指的是另一个大混乱。


这有意义吗?



wrt / all Thing实例必须引用同一个Stack实例,

有一个非常明显的答案:make stack instance一个属性

class Thing,即:


class Thing(object):

stack = Stack()


def some_method(self,val):

self.stack.push(val)

#etc ......


这样你可以直接从Thing类访问这个堆栈,因为它可以从任何实例,子类和子类实例中获取。


现在不清楚的是Stack

和Canvas之间的确切关系。你没有给出任何答案,建议或

提示感觉的足够细节。


HTH


>我认为这可能是多重继承的情况


???



好​​吧,就Canvas和Thing从Stack继承而言因此

(不知何故,不确定如何)他们都可以访问到Stack.stack(列表)


wrt / all Thing实例必须引用同一个Stack实例,

there''sa非常明显的答案:使Stack实例成为

类的属性Thing,即:

class Thing(object):

stack = Stack( )


def some_method(self,val):

self.stack.push(val)

#etc ...



无法做到:

Canvas --- Stack< --- Thing

两个画布和事情必须使用相同的堆栈。它将两个东西推到了



现在不清楚的一点是Stack之间的确切关系

和Canvas。你没有提供足够的细节任何答案,建议或

提示做出感觉。



对不起,不想写一篇过长的帖子。


a Canvas拥有很多东西(图形)和它将每个Thing推到了

堆栈上。事物也将数据推送到同一个堆栈。之后,Stack

弹出并将每个Thing绘制到屏幕上。


我要问的是微妙的,我不知道如何说出来:怎么可以

类在不使用全局变量的情况下共享公共对象

在其中命名?

## ==另一个API模块也许===

班级:

def推(自我,东西):

通过


Class Canvas:

def do(self):

s.push(" data)#I'对''s'感觉不对'这里。


Class Thing:

def buzz(self):

print s.pop(0)


## ==用户空间代码区===

s = Stack()#我想避免直接命名为''s''

c = Canvas()

c.push(" bozo")

t = Thing()

t.buzz()


希望更有意义。

\ n


Donn Ingleaécrit:


>>>我认为这可能是多重继承的情况

???



嗯,就Canvas和Thing继承Stack而言因此

(不知何故,不确定如何)他们都可以访问到Stack.stack(列表)


>> wrt / all Thing实例必须引用同一个Stack实例,
有一个非常明显的答案:使Stack实例成为类Thing的属性,即:
class Thing(object):
stack = Stack()
def some_method(self,val):
self.stack.push(val)
#etc ...




不能这样做:

Canvas --- Stack< --- Thing



抱歉,你的例子是使用课程,不是实例。所以虽然*你*

可能知道你的意思,但这对我来说是不可判定的。


Canvas和Thing都必须使用相同的堆栈。



你的意思是:Canvas类和Thing类都必须使用相同的Stack

类?或者:对于所有Canvas实例和所有Thing实例,

必须只是一个相同的Stack实例?或者:一个给定的(Canvas实例,

Thing实例)一对必须共享一个相同的Stack实例?或者(等等......)


将它们推到

它们两者之间。



两者都是什么?上课?实例? - )


>


>>现在不清楚的是Stack
和Canvas之间的确切关系。你没有提供任何答案,建议或提示感觉的足够细节。



对不起,我不想写一篇过长的帖子。



在过于模糊和过度支付
之间肯定存在平衡! - )


a Canvas拥有很多东西(图形),它将每个东西推到了

堆栈上。事物也将数据推送到同一个堆栈。之后,Stack

弹出并将每个Thing绘制到屏幕上。


我要问的是微妙的,我不知道如何单词:怎么可以




我猜你的意思是实例,而不是类。


在不使用全局变量的情况下共享公共对象

在其中命名?



Err ......也许是一个愚蠢的问题,但是传递常见的

对象呢?到初始化器?


## ==另一个模块中的API也许===

类别堆栈:

def push(self,stuff):

pass


Class Canvas:

def do(self):

s.push(" data)#我在这里感觉不对。


Class Thing:

def buzz(self):

print s.pop(0)

## ==用户空间代码区===

s = Stack()#I我想避免直接命名为''s''

c = Canvas()

c.push(" bozo" )

t =东西()

t.buzz()



#API土地

类Stack(对象):

#ok,我们都知道堆栈是什么


类Canvas(对象):

def __init __(self,stack):

self.stack = stack

def do(self):

self.stack。 push(data)


class Thing(object):

def __init__ (自我,堆叠):

self.stack = stack

def buzz(self):

print self.stack.pop(0)

#Userland

s = Stack()

c =画布

t = Thing(s)


c.do()

t.buzz()


HTH


Hi,
I''m getting myself really confused. I have three classes. Two of them need
to reference the third, like so:

Canvas ---Stack <--- Thing

I am doing this right now:

s = Stack()

And then within <class Canvas:I am referring directly to the global
variable ''s'' (and again from Thing).

Now, this works, but it bugs me somewhat. I don''t want to limit a user to
having to declare a global variable of a particular name.
They should be able to say <Zombie = Stack()>

This may also leads to more confusion because the user can also make Things
directly <mything = Thing()and I want the new things to all reference the
*same* Stack. (Right now it works because I reference ''s'' within Thing.)

I thought this might be a case for multiple inheritance but I just keep
getting recursion errors as one thing refers to another in a big mess.

Does that makes sense?

\d

解决方案

Donn Ingle a écrit :

Hi,
I''m getting myself really confused. I have three classes. Two of them need
to reference the third, like so:

Canvas ---Stack <--- Thing

I am doing this right now:

s = Stack()

And then within <class Canvas:I am referring directly to the global
variable ''s'' (and again from Thing).

Now, this works, but it bugs me somewhat.

Indeed...

I don''t want to limit a user to
having to declare a global variable of a particular name.
They should be able to say <Zombie = Stack()>

This may also leads to more confusion because the user can also make Things
directly <mything = Thing()and I want the new things to all reference the
*same* Stack. (Right now it works because I reference ''s'' within Thing.)

I thought this might be a case for multiple inheritance

???

but I just keep
getting recursion errors as one thing refers to another in a big mess.

Does that makes sense?

wrt/ all Thing instances having to refer to a same Stack instance,
there''s a pretty obvious answer: make the Stack instance an attribute of
class Thing, ie:

class Thing(object):
stack = Stack()

def some_method(self, val):
self.stack.push(val)
# etc...

This way you can access this stack from the Thing class directly, as
well as from any instance, subclass and subclass instance of.

Now the point that isn''t clear is the exact relationship between Stack
and Canvas. You didn''t give enough details for any answer, advice or
hint to make sens.

HTH


>I thought this might be a case for multiple inheritance

???

Well, in terms of having Canvas and Thing inherit from Stack and thereby
(somehow, not sure how) they would both have access to Stack.stack (a list)

wrt/ all Thing instances having to refer to a same Stack instance,
there''s a pretty obvious answer: make the Stack instance an attribute of
class Thing, ie:
class Thing(object):
stack = Stack()

def some_method(self, val):
self.stack.push(val)
# etc...

No can do:
Canvas ---Stack <--- Thing
Both Canvas and Thing have to use the same Stack. It gets things pushed onto
it by them both.

Now the point that isn''t clear is the exact relationship between Stack
and Canvas. You didn''t give enough details for any answer, advice or
hint to make sens.

Sorry, didn''t want to write an overly long post.

a Canvas holds many Things (graphics) and it pushes each Thing onto the
Stack. The Things also push data onto the same Stack. After that the Stack
pops and draws each Thing to the screen.

What I''m asking about is subtle and I don''t know how to word it: how can
Classes share common objects without using global variables specifically
named within them?
## == API in another module perhaps ===
Class Stack:
def push(self,stuff):
pass

Class Canvas:
def do(self):
s.push("data") #I don''t feel right about ''s'' here.

Class Thing:
def buzz(self):
print s.pop(0)

## == User space code area ===
s = Stack() #I want to avoid this direct naming to ''s''
c = Canvas()
c.push("bozo")
t = Thing()
t.buzz()

Hope that makes more sense.
\d


Donn Ingle a écrit :

>>>I thought this might be a case for multiple inheritance


???


Well, in terms of having Canvas and Thing inherit from Stack and thereby
(somehow, not sure how) they would both have access to Stack.stack (a list)

>>wrt/ all Thing instances having to refer to a same Stack instance,
there''s a pretty obvious answer: make the Stack instance an attribute of
class Thing, ie:
class Thing(object):
stack = Stack()

def some_method(self, val):
self.stack.push(val)
# etc...



No can do:
Canvas ---Stack <--- Thing

Sorry but you''re exemple is using classes, not instances. So while *you*
may know what you mean, it''s not decidable for me.

Both Canvas and Thing have to use the same Stack.

You mean: both Canvas class and Thing class have to use the same Stack
Class ? Or : for all Canvas instances and all Thing instances, there
must be only one same Stack instance ? Or: a given (Canvas instance,
Thing instance) couple must share a same Stack instance ? Or (etc...)

It gets things pushed onto
it by them both.

Both what ? classes ? instances ?-)

>

>>Now the point that isn''t clear is the exact relationship between Stack
and Canvas. You didn''t give enough details for any answer, advice or
hint to make sens.


Sorry, didn''t want to write an overly long post.

There''s certainly a balance between being overly vague and being overly
long !-)

a Canvas holds many Things (graphics) and it pushes each Thing onto the
Stack. The Things also push data onto the same Stack. After that the Stack
pops and draws each Thing to the screen.

What I''m asking about is subtle and I don''t know how to word it: how can
Classes

I guess you mean "instances", not "classes".

share common objects without using global variables specifically
named within them?

Err...Perhaps a dumb question, but what about passing the "common
objects" to initializers ?

## == API in another module perhaps ===
Class Stack:
def push(self,stuff):
pass

Class Canvas:
def do(self):
s.push("data") #I don''t feel right about ''s'' here.

Class Thing:
def buzz(self):
print s.pop(0)

## == User space code area ===
s = Stack() #I want to avoid this direct naming to ''s''
c = Canvas()
c.push("bozo")
t = Thing()
t.buzz()

# API land
class Stack(object):
# ok, we all know what a stack is

class Canvas(object):
def __init__(self, stack):
self.stack = stack
def do(self):
self.stack.push("data")

class Thing(object):
def __init__(self, stack):
self.stack = stack
def buzz(self):
print self.stack.pop(0)
# Userland
s = Stack()
c = Canvas(s)
t = Thing(s)

c.do()
t.buzz()

HTH


这篇关于类中的全局变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆