Python 2.3.3中的类封装错误 [英] Class Encapsulation Errors in Python 2.3.3

查看:57
本文介绍了Python 2.3.3中的类封装错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我的代码中有一些错误,我找不到原因在这里

是错误


i有3个类:

-Song

-Album

-Artist


Song有3条数据带吸气器和setter meathods:

-Name |即歌曲名称

-Album

-Artist


专辑有3条数据:

- 歌曲|一个Song()实例列表

-Name

-Artist


艺术家有4个数据:

-Name

-Songs | Song()实例列表

-Albums | Album()实例列表

-unknownAlbum |如果没有一张

专辑,那么这张专辑会被放入专辑

现在当我将歌曲添加到多张专辑中时会出现问题,例如

这个:

代码:

---------------------------- --------------------

s =歌曲(''歌曲1'')

s2 =歌曲( ''歌曲2'')


a =专辑(''嘿'')

a.addSong(s)

ab =专辑(''yeayea'')

打印a

打印ab

ab.addSong(s2)

打印

打印一份

打印ab

------------------- -----------------------------

输出:

*** *********************************************

嘿嘿歌曲嘿嘿|

yeayea歌曲1嘿嘿|


嘿歌曲1嘿|歌曲2 yeayea |

yeayea song 1嘿|歌曲2 yeayea |

************************************** **********


嘿专辑有歌曲1。因为它确实如此。

" yeayea"即使在歌曲1时也是如此。被放在专辑嘿中''yeayea''

甚至还没有存在。


为什么会这样。我检查了每个

实例的实际内存参考值,它们是不同的,这些由于某些莫名其妙的原因而被联系起来的不同

条款的链接。


有没有人知道为什么会发生这种情况以及如何解决这个问题?


欢呼

Tim Henderson

Hi i have some errors in my code that i can''t find the reason for here
is the error

i have 3 classes:
-Song
-Album
-Artist

Song has 3 pieces of data with getter and setter meathods:
-Name | i.e. song name
-Album
-Artist

Album has 3 pieces of data:
-Songs | a list of Song() instances
-Name
-Artist

Artist has 4 pieces of Data:
-Name
-Songs | a list of Song() instances
-Albums |a list of Album() instances
-unknownAlbum | the album a song is put into if it doesn''t have an
album
now the problem arises when i add songs into multiple albums like
this:
Code:
------------------------------------------------
s = Song(''song 1'')
s2 = Song(''song 2'')

a = Album(''hey'')
a.addSong(s)
ab = Album(''yeayea'')
print a
print ab
ab.addSong(s2)
print
print a
print ab
------------------------------------------------
Output:
************************************************
hey song 1 hey |
yeayea song 1 hey |

hey song 1 hey | song 2 yeayea |
yeayea song 1 hey | song 2 yeayea |
************************************************

the "hey" album has "song 1" as it is suppose to however so does
"yeayea" even though when "song 1" was put in the album "hey" ''yeayea''
didn''t even exist yet.

Why is this happening. I checked the actually memory refrence for each
instance and they are different, these supposidly isolated different
intances of clases are linked for some inexplicable reason.

Does any one know why this is happening and how i can fix it?

cheers
Tim Henderson

推荐答案

Tim Henderson写道:
Tim Henderson wrote:
您好我的代码中有一些错误,我找不到这里的原因
是错误
[...]
嘿专辑有歌曲1。然而,假设它是如此。
yeayea即使在歌曲1时也是如此。被放在专辑嘿中''yeayea''
甚至还没有存在。
Hi i have some errors in my code that i can''t find the reason for here
is the error
[...]
the "hey" album has "song 1" as it is suppose to however so does
"yeayea" even though when "song 1" was put in the album "hey" ''yeayea''
didn''t even exist yet.




我可以想到两个可能的原因。

第一个是你将歌曲列表作为一个类属性存储,

而不是实例属性。第二个是你在方法调用中使用了

可变的默认值。 (我猜它可能是第二个原因。)


你有类似的东西吗?


class专辑:

def __init __(self,songs = []):

self.songs = songs

# ...


这里的问题是函数/方法中的默认值是* *
在* compile *时计算。这意味着这会创建一个单独的
空列表,该列表用作* every * Album

实例的默认值。当你改变那个列表时,每个带有

引用该列表的实例都会显示更改。


另一种可能性(类属性而不是实例属性)

会发生这样的事情:


class专辑:

songs = []

def __init __(self,...):

#...


在这种情况下,再次,你有一个空列表将会所有专辑实例共享




在这种情况下你可能想要做的是:


class专辑:

def __init __(自我,歌曲=无)

如果歌曲为无:

songs = []

self.songs =歌曲

#...


这将允许你在

初始化程序,但仍然确保每个实例都获得一个独立的空白列表,如果没有传入任何内容。


Jeff Shannon

技师/ Pr ogrammer

Credit International



I can think of two likely reasons offhand.

The first is that you''re storing the songlist as a class attribute,
rather than as an instance attribute. The second is that you''re using a
mutable default value in a method call. (I''m guessing that it''s
probably this second reason.)

Do you have something like this?

class Album:
def __init__(self, songs=[]):
self.songs = songs
# ...

The problem here is that default values in functions/methods are
evaluated at *compile* time. That means that this creates a single
empty list, which is used as the default value for *every* Album
instance. When you mutate that list, then every instance with a
reference to that list shows the changes.

The other possibility (class attribute rather than instance attribute)
would happen if you have something like this:

class Album:
songs = []
def __init__(self, ...):
# ...

In this case, again, you have a single empty list that will end up being
shared by all Album instances.

What you probably want to do in this case is this:

class Album:
def __init__(self, songs=None)
if songs is None:
songs = []
self.songs = songs
# ...

This will allow you to have an optional songs parameter in the
initializer, but still ensure that every instance gets an independent
empty list if nothing''s passed in.

Jeff Shannon
Technician/Programmer
Credit International


Tim Henderson写道:


< snip>
Tim Henderson wrote:

<snip>

现在当我将歌曲添加到多个专辑中时会出现问题,例如
这个:
代码:
---------- --------------------------------------
s =歌曲(''歌曲1 '')
s2 =歌曲(''歌曲2'')

a =专辑(''嘿'')
a.addSong(s)
ab =专辑(''yeayea'')
打印
打印ab
ab.addSong(s2)
打印
打印
打印ab
----------------------------------------------- -
产出:
*************************************** *********
嘿歌1嘿|
yeayea song 1嘿嘿|

嘿歌1嘿|歌曲2 yeayea |
yeayea song 1嘿|歌曲2 yeayea |
****************************************** ******

now the problem arises when i add songs into multiple albums like
this:
Code:
------------------------------------------------
s = Song(''song 1'')
s2 = Song(''song 2'')

a = Album(''hey'')
a.addSong(s)
ab = Album(''yeayea'')
print a
print ab
ab.addSong(s2)
print
print a
print ab
------------------------------------------------
Output:
************************************************
hey song 1 hey |
yeayea song 1 hey |

hey song 1 hey | song 2 yeayea |
yeayea song 1 hey | song 2 yeayea |
************************************************



< snip>

定义addSong()有助于确定真正的原因。在这里猜猜

,但看起来你存储歌曲的''列表'是

在所有专辑中共享。通常情况下,如果您在类本身中定义列表

使其成为类属性 - 而不是在__init __()中。

请向我们显示相册的代码以帮助你进一步。


Shalabh


<snip>

Defnition of addSong() would help in determining the real cause. Taking
a guess here but it looks like the ''list'' you store the songs in is
shared across all albums. Typically this happens if you define the list
in the class itself making it a class attribute - and not in __init__().
Please show us the code of Album to help you further.

Shalabh


请发布Album类的实现。问题来自

可能来自定义或初始化歌曲列表的方式。也就是说,定义或初始化可能导致分配给歌曲的列表对象被所有Album实例共享。

但是我(并且

可能没有其他人)可以在没有查看

代码的情况下准确说出问题所在。


Dan


" Tim Henderson" < TI ****** @ gmail.com>在消息中写道

news:47 ************************** @ posting.google.c om ...
Please post the implementation of the Album class. The problem is coming
probably from the way the Songs list is defined or initialized. That is,
the definition or the initialization probably cause the list object that is
assigned to Songs to be shared by all the Album instances. But I (and
likely no one else) can say exactly what the problem is without seing the
code.

Dan

"Tim Henderson" <ti******@gmail.com> wrote in message
news:47**************************@posting.google.c om...
您好我的代码中有一些错误,我找不到原因在这里
是错误

我有3个类:
-Song
-Album
-Artist

Song有3个数据包含getter和setter meathods:
-Name |即歌名
-Album
-Artist

专辑有3条数据:
-Songs |一个Song()实例列表
-Name
-Artist

艺术家有4个数据:
-Name
-Songs | Song()实例列表
-Albums | Album()实例列表
-unknownAlbum |如果没有一张专辑,专辑中会有一首歌。现在当我将歌曲添加到多张专辑中时会出现问题,例如
这个:
代码:
-------------------------------------------- ----
s =歌曲(''歌曲1'')
s2 =歌曲(''歌曲2'')

a =专辑(''嘿' ')
a.addSong(s)
ab =专辑(''yeayea'')
打印
打印ab
ab.addSong(s2)打印ab
------------------------------- -----------------
输出:
*********************** *************************
嘿歌1嘿|
yeayea song 1嘿|
嘿歌1嘿|歌曲2 yeayea |
yeayea song 1嘿|歌曲2 yeayea |
****************************************** ******

嘿专辑有歌曲1。然而,假设它是如此。
yeayea即使在歌曲1时也是如此。被放在专辑嘿中''yeayea''
甚至还没有存在。

为什么会这样。我检查了每个
实例的实际内存参考,并且它们是不同的,这些由于某些莫名其妙的原因而被联系起来的不同的相互关联的内容。

有没有人知道为什么这个正在发生什么以及如何解决这个问题?

Tim Henderson
Hi i have some errors in my code that i can''t find the reason for here
is the error

i have 3 classes:
-Song
-Album
-Artist

Song has 3 pieces of data with getter and setter meathods:
-Name | i.e. song name
-Album
-Artist

Album has 3 pieces of data:
-Songs | a list of Song() instances
-Name
-Artist

Artist has 4 pieces of Data:
-Name
-Songs | a list of Song() instances
-Albums |a list of Album() instances
-unknownAlbum | the album a song is put into if it doesn''t have an
album
now the problem arises when i add songs into multiple albums like
this:
Code:
------------------------------------------------
s = Song(''song 1'')
s2 = Song(''song 2'')

a = Album(''hey'')
a.addSong(s)
ab = Album(''yeayea'')
print a
print ab
ab.addSong(s2)
print
print a
print ab
------------------------------------------------
Output:
************************************************
hey song 1 hey |
yeayea song 1 hey |

hey song 1 hey | song 2 yeayea |
yeayea song 1 hey | song 2 yeayea |
************************************************

the "hey" album has "song 1" as it is suppose to however so does
"yeayea" even though when "song 1" was put in the album "hey" ''yeayea''
didn''t even exist yet.

Why is this happening. I checked the actually memory refrence for each
instance and they are different, these supposidly isolated different
intances of clases are linked for some inexplicable reason.

Does any one know why this is happening and how i can fix it?

cheers
Tim Henderson



这篇关于Python 2.3.3中的类封装错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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