循环类逻辑 [英] Circular Class Logic

查看:49
本文介绍了循环类逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组描述文件,文件夹等的类,我经常在我的脚本中使用

来移动文件,获取文件

扩展名,转换路径,更改权限等等。这非常类似于Jason Orendorff的路径库,对于我来说非常有用。基类''Data.py''存储磁盘上任何

数据的所有基本属性,然后将其子类化为代表文件

和包含文件夹的文件夹特定于该类型对象的属性。


我最近创建了一个新类''Disk.py'',它是

''文件夹的子类.py'',用于描述连接到计算机的任何本地或网络

磁盘的所有属性。我将它从Folder子类化,因为

它确实是一个文件夹,它只是在一个驱动器的根目录。我知道这些''磁盘''对象非常有用,而且我想出了一个''磁盘'实例的想法基类中的共享类变量

(singleton?)(''Data'')想法是任何文件或Folder对象的

实例都包含附加到系统的Disk

对象列表,可以相应地使用它们。另外

重要的是我只需要为任何给定的运行应用程序收集一次磁盘信息

并且所有实例都可以使用它

(原来我正在获取每个文件/文件夹对象的磁盘信息)


经过一个小时的敲击我的头来获得AttributeError:

module''对象没有属性''Disks''"错误,我终于意识到

我试图在基类中包含一个实例,它是一个

子类。如果没有让''Disk''不再是

文件夹的子类,还有其他方法可以在该对象的

基类中包含一个子类化实例吗? (这很难说出来)


~肖恩

解决方案

ha**********@gmail.com 写道:
< blockquote class =post_quotes>
没有让''Disk''不再是Folder的子类,是否有任何

的其他方式在子类中包含一个子类实例

对象? (这很难说出来)



想象你所描述的内容有点困难,但是IIUC

你的问题是这种形式:


===== foo.py =====

导入栏


类Foo(bar.Bar):

通过

=====


== === bar.py =====

import baz


class Bar(baz.Baz):

通过

=====


===== baz.py =====

import foo


班级Baz(foo.Foo):

通过

=====


也就是说,每个类都希望从其他类继承。


这些情况下的通常解决方案是找到常见的所需

功能和这个因素是一个单独的类,然后是两个现有类的

超类,打破了这个圈子。


===== wibble。 py =====

#无依赖性


class Wibble(对象):

通过

=====


===== foo.py =====

导入wibble

导入栏

类Foo(wibble.Wibble,bar.Bar):

通票

=====


===== baz.py =====

import wibble


班级Baz(wibble.Wibble):

通过

=====

请注意,Baz不再是foo.Foo的子类,并且Foo和Baz都可以获得他们从wibble.Wibble共享的常用功能。


-

\买不到你想要的东西,但你需要的东西;你不需要的东西|

` \一分钱很贵。 - Cato,234-149 BC,Relique |

_o__)|

Ben Finney


>


也就是说,每个类都希望从其他类继承。



这不是我正在做的,但你的评论仍然可能会有所帮助。

我其实想要包含一个它的子类实例是

这样的超类:


===== foo.py =====

import Baz


class Foo:

baz = Baz.Baz()


def __init__ (个体经营):

通过

=====


===== bar.py ==== =

进口Foo


类酒吧(Foo.Foo):

通过

== ===


===== baz.py =====

进口酒吧


Baz(Bar.Bar):

通过

=====


通常的解决方案这些情况是找到共同要求的功能,并将其分解为一个单独的类,然后是两个现有类的

超类,打破了这个圈。 br />

===== wibble.py =====

#no dependden cies


class Wibble(对象):

pass

=====


===== foo.py =====

导入wibble

导入栏

类Foo (wibble.Wibble,bar.Bar):

通过

=====


===== baz .py =====

导入wibble


类Baz(wibble.Wibble):

通过

=====


请注意,Baz不再是foo.Foo的子类,Foo和Baz都可以获得他们共享的常用功能

来自wibble.Wibble。



我必须考虑一下,看看

是否有意义。谢谢你的想法。考虑到上面的描述,这是最好的

解决方案吗?


~Sean


> ;

-

\买不到你想要的,但你需要什么;你不需要的东西|

` \一分钱很贵。 - Cato,234-149 BC,Relique |

_o__)|

Ben Finney



< half.italian< atgmail.comwrites:


这不是我正在做的,但你的评论仍然可能帮助。

我实际上想在其中包含一个子类的实例'

这样的超类:



将初始化移动到另一个函数。


>

===== foo.py =====

#import Baz



删除
以上的行


>

类Foo:

#baz = Baz.Baz()



删除
以上的行< blockquote class =post_quotes>
>

def __init __(self):

pass

=====


并在下面添加:

def initFoo():

import baz

Foo.baz = baz.Baz()

initFoo()


bar.py,baz.py没有变化

而不是initFoo,你可以使用自定义元类。或者是一个类装饰器(如果

,当它们变得可用时......)

上面的代码是一种有效的方式来做你想要的。但是我会考虑实际需要做这些事情 - 你确定这是一个好的设计吗?


-

Gabriel Genellina


I have a set of classes that describe Files, Folders, etc., that I use
often in my scripts for moving files around, getting a files
extension, converting paths, changing permissions, etc It''s very
similar to Jason Orendorff''s ''Path'' library, and is very useful to
me. The base class ''Data.py'' stores all the basic attributes of any
piece of data on disk, and it is then subclassed to represent files
and folders with the attributes specific to that type of object.

I recently made a new class ''Disk.py'' that is a subclass of
''Folder.py'', to describe all of the attributes of any local or network
disks attached to the computer. I subclassed it from Folder, because
it really is a folder, it''s just at the root of a drive. I''m
realizing that these ''Disk'' objects are pretty useful, and I came up
with the idea to put a ''Disks'' instance as a shared class variable
(singleton?) within the base class (''Data'') The idea being that any
instance of a File, or Folder object would contain the list of Disk
objects attached to the system, and could use them accordingly. Also
important is that I would only need to gather the disk info once for
any given running application and all the instances could use it
(origianlly I was getting the disk info for each File/Folder object)

After an hour of banging my head from getting "AttributeError:
''module'' object has no attribute ''Disks''" errors, I finally realized
that I''m trying to include an instance in the base class that is a
subclass of itself. Short of making ''Disk'' no longer a subclass of
Folder, is there any other way to include a subclassed instance in the
base class of that object? (this is very hard to put in to words)

~Sean

解决方案

ha**********@gmail.com writes:

Short of making ''Disk'' no longer a subclass of Folder, is there any
other way to include a subclassed instance in the base class of that
object? (this is very hard to put in to words)

It''s a little difficult to visualise what you''re describing, but IIUC
your problem is of this form:

===== foo.py =====
import bar

class Foo(bar.Bar):
pass
=====

===== bar.py =====
import baz

class Bar(baz.Baz):
pass
=====

===== baz.py =====
import foo

class Baz(foo.Foo):
pass
=====

That is, each of the classes want to inherit from the others.

The usual solution in these cases is to find the common required
functionality and factor that out to a separate class that is then the
superclass of two of the existing classes, breaking the circle.

===== wibble.py =====
# no dependencies

class Wibble(object):
pass
=====

===== foo.py =====
import wibble
import bar

class Foo(wibble.Wibble, bar.Bar):
pass
=====

===== baz.py =====
import wibble

class Baz(wibble.Wibble):
pass
=====

Note that Baz no longer subclasses foo.Foo, and both Foo and Baz get
the common functionality they share from wibble.Wibble.

--
\ "Buy not what you want, but what you need; what you do not need |
`\ is expensive at a penny." -- Cato, 234-149 BC, Relique |
_o__) |
Ben Finney


>

That is, each of the classes want to inherit from the others.

That''s not exactly what I''m doing, but your comment still might help.
I actually want to include an instance of a subclass in it''s
superclass like this:

===== foo.py =====
import Baz

class Foo:
baz = Baz.Baz()

def __init__(self):
pass
=====

===== bar.py =====
import Foo

class Bar(Foo.Foo):
pass
=====

===== baz.py =====
import Bar

class Baz(Bar.Bar):
pass
=====

The usual solution in these cases is to find the common required
functionality and factor that out to a separate class that is then the
superclass of two of the existing classes, breaking the circle.

===== wibble.py =====
# no dependencies

class Wibble(object):
pass
=====

===== foo.py =====
import wibble
import bar

class Foo(wibble.Wibble, bar.Bar):
pass
=====

===== baz.py =====
import wibble

class Baz(wibble.Wibble):
pass
=====

Note that Baz no longer subclasses foo.Foo, and both Foo and Baz get
the common functionality they share from wibble.Wibble.

I have to think about that for a bit and see if it makes sense to
factor anything out. Thanks for the idea. Would that be the best
solution considering the above description?

~Sean

>
--
\ "Buy not what you want, but what you need; what you do not need |
`\ is expensive at a penny." -- Cato, 234-149 BC, Relique |
_o__) |
Ben Finney



<half.italian <atgmail.comwrites:

That''s not exactly what I''m doing, but your comment still might help.
I actually want to include an instance of a subclass in it''s
superclass like this:

Move the initialization to another function.

>
===== foo.py =====
# import Baz

Remove the line above

>
class Foo:
# baz = Baz.Baz()

Remove the line above

>
def __init__(self):
pass
=====

and add this below:
def initFoo():
import baz
Foo.baz = baz.Baz()
initFoo()

No changes in bar.py, baz.py
Instead of initFoo, you could use a custom metaclass. Or a class decorator (if
and when they become available...)
The code above is an effective way of doing what you want. But I''d think about
the actual need of doing such things - are you sure it''s a good design?

--
Gabriel Genellina


这篇关于循环类逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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