2006年11月05日 星期日 13:24
在django/contrib/syndication/feeds.py中:
关于feed类的定义
class Feed(object):
item_pubdate = None
item_enclosure_url = None
feed_type = feedgenerator.DefaultFeed
title_template = None
description_template = None
def __init__(self, slug, feed_url):
self.slug = slug
self.feed_url = feed_url
self.title_template_name = self.title_template or ('feeds/%s_title.html'
% slug)
self.description_template_name = self.description_template or
('feeds/%s_description.html' % slug)
在__init__函数中title_template_name,description_template_name的赋值为什么要
做一下判断,在前面不是已经定义title_template = None了么,
self.title_template_name = self.title_template or ('feeds/%s_title.html'
% slug)不就是
self.title_template_name = 'feeds/%s_title.html' % slug,
为什么还要在or前面判断一下?
2006年11月06日 星期一 09:08
On 11/5/06, leopay <leopay在gmail.com> wrote: > 在django/contrib/syndication/feeds.py中: > 关于feed类的定义 > > class Feed(object): > item_pubdate = None > item_enclosure_url = None > feed_type = feedgenerator.DefaultFeed > title_template = None > description_template = None > > def __init__(self, slug, feed_url): > self.slug = slug > self.feed_url = feed_url > self.title_template_name = self.title_template or ('feeds/%s_title.html' > % slug) > self.description_template_name = self.description_template or > ('feeds/%s_description.html' % slug) > > > 在__init__函数中title_template_name,description_template_name的赋值为什么要 > 做一下判断,在前面不是已经定义title_template = None了么, > self.title_template_name = self.title_template or ('feeds/%s_title.html' > % slug)不就是 > self.title_template_name = 'feeds/%s_title.html' % slug, > 为什么还要在or前面判断一下? 我想这是因为Feed一般是作为基类,真正的处理需要你从它进行派生,因此有可能你在新的子类中就定义了这两个变量,如果你不重载__init__的话,它这样判断比较安全。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年11月06日 星期一 12:21
子类有可能把这个None改掉了 2006/11/5, leopay <leopay在gmail.com>: > > 在django/contrib/syndication/feeds.py中: > 关于feed类的定义 > > class Feed(object): > item_pubdate = None > item_enclosure_url = None > feed_type = feedgenerator.DefaultFeed > title_template = None > description_template = None > > def __init__(self, slug, feed_url): > self.slug = slug > self.feed_url = feed_url > self.title_template_name = self.title_template or ('feeds/%s_title.html' > % slug) > self.description_template_name = self.description_template or > ('feeds/%s_description.html' % slug) > > > 在__init__函数中title_template_name,description_template_name的赋值为什么要 > 做一下判断,在前面不是已经定义title_template = None了么, > self.title_template_name = self.title_template or ('feeds/%s_title.html' > % slug)不就是 > self.title_template_name = 'feeds/%s_title.html' % slug, > 为什么还要在or前面判断一下? > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- 茫茫人海,你是我的最爱 -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061106/2043b197/attachment.htm
2006年11月06日 星期一 15:28
class Feed(object):
> > item_pubdate = None
> > item_enclosure_url = None
> > feed_type = feedgenerator.DefaultFeed
> > title_template = None
> > description_template = None
> >
> > def __init__(self, slug, feed_url):
> > self.slug = slug
> > self.feed_url = feed_url
> > self.title_template_name = self.title_template or ('feeds/%s_title.html'
> > % slug)
> > self.description_template_name = self.description_template or
> > ('feeds/%s_description.html' % slug)
第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学.
On 11/6/06, 大熊 <bearsprite在gmail.com> wrote:
> 子类有可能把这个None改掉了
>
> 2006/11/5, leopay <leopay在gmail.com>:
> > 在django/contrib/syndication/feeds.py中:
> > 关于feed类的定义
> >
> > class Feed(object):
> > item_pubdate = None
> > item_enclosure_url = None
> > feed_type = feedgenerator.DefaultFeed
> > title_template = None
> > description_template = None
> >
> > def __init__(self, slug, feed_url):
> > self.slug = slug
> > self.feed_url = feed_url
> > self.title_template_name = self.title_template or ('feeds/%s_title.html'
> > % slug)
> > self.description_template_name = self.description_template or
> > ('feeds/%s_description.html' % slug)
> >
> >
> >
> 在__init__函数中title_template_name,description_template_name的赋值为什么要
> > 做一下判断,在前面不是已经定义title_template = None了么,
> > self.title_template_name = self.title_template or ('feeds/%s_title.html'
> > % slug)不就是
> > self.title_template_name = 'feeds/%s_title.html' % slug,
> > 为什么还要在or前面判断一下?
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
> --
> 茫茫人海,你是我的最爱
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
--
蔡峰 Cai Feng
2006年11月06日 星期一 15:34
On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > class Feed(object): > > > item_pubdate = None > > > item_enclosure_url = None > > > feed_type = feedgenerator.DefaultFeed > > > title_template = None > > > description_template = None > > > > > > def __init__(self, slug, feed_url): > > > self.slug = slug > > > self.feed_url = feed_url > > > self.title_template_name = self.title_template or ('feeds/%s_title.html' > > > % slug) > > > self.description_template_name = self.description_template or > > > ('feeds/%s_description.html' % slug) > > 第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学. > 这种用法体现了python的动态特性。在处理self.attribute时,如果要处理的是一个实例,那么python先在实例的属性中查找是否有attribute这个属性,如果没有,则查找对应的类属性是否有,如果还有没有,再按基类顺序来查找基类中的这个属性,如果还有没,则处理特殊的类方法,如__getattr__或__getattribute__(用在newstyleclass中),再没有就抛异常了。 所以self.attribute当实例属性不存在时会自动引用类属性。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年11月06日 星期一 23:41
呵呵,解释的很详细,谢谢.
我把类里面的attribute当成java中的staic属性的内容了.
把__init__中的self.attribute当成java中的一般属性了.
另外我还有一个疑问,
在javascript中也有
function a(){
this.attribute=1;
attribute=2;
}
这两个attribute使用时我也不太明白,不知道能否解释一下?
On 11/6/06, limodou <limodou在gmail.com> wrote:
> On 11/6/06, cai feng <caifen1985在gmail.com> wrote:
> > class Feed(object):
> > > > item_pubdate = None
> > > > item_enclosure_url = None
> > > > feed_type = feedgenerator.DefaultFeed
> > > > title_template = None
> > > > description_template = None
> > > >
> > > > def __init__(self, slug, feed_url):
> > > > self.slug = slug
> > > > self.feed_url = feed_url
> > > > self.title_template_name = self.title_template or ('feeds/%s_title.html'
> > > > % slug)
> > > > self.description_template_name = self.description_template or
> > > > ('feeds/%s_description.html' % slug)
> >
> > 第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学.
> >
> 这种用法体现了python的动态特性。在处理self.attribute时,如果要处理的是一个实例,那么python先在实例的属性中查找是否有attribute这个属性,如果没有,则查找对应的类属性是否有,如果还有没有,再按基类顺序来查找基类中的这个属性,如果还有没,则处理特殊的类方法,如__getattr__或__getattribute__(用在newstyleclass中),再没有就抛异常了。
>
> 所以self.attribute当实例属性不存在时会自动引用类属性。
>
> --
> I like python!
> UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
> My Blog: http://www.donews.net/limodou
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
--
蔡峰 Cai Feng
2006年11月07日 星期二 01:03
在javascript的例子中,a应当是一个对象初创化函数,或者是一个可以赋值给其它对象的属性。 那么this.attribute=1中的this表示它创建的对象或依附对象,表达式将该对象的attribute属性赋值为1。比如 oA=new a()或者oB.fn=a;oB.fn(); 执行后,oA.attribute和oB.attribute的值就成为1。 至于attribute2=2,则仅仅是对attribute进行赋值,这个attribute只是个变量,与上述的对象没有什么关系。注意到你给的例子没有var声明,该变量应当是该函数外的变量(全局变量,或包含a函数的域的局部变量)。 On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > > 呵呵,解释的很详细,谢谢. > 我把类里面的attribute当成java中的staic属性的内容了. > 把__init__中的self.attribute当成java中的一般属性了. > > 另外我还有一个疑问, > 在javascript中也有 > function a(){ > this.attribute=1; > attribute=2; > } > 这两个attribute使用时我也不太明白,不知道能否解释一下? > > On 11/6/06, limodou <limodou在gmail.com> wrote: > > On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > > > class Feed(object): > > > > > item_pubdate = None > > > > > item_enclosure_url = None > > > > > feed_type = feedgenerator.DefaultFeed > > > > > title_template = None > > > > > description_template = None > > > > > > > > > > def __init__(self, slug, feed_url): > > > > > self.slug = slug > > > > > self.feed_url = feed_url > > > > > self.title_template_name = self.title_template or > ('feeds/%s_title.html' > > > > > % slug) > > > > > self.description_template_name = self.description_template or > > > > > ('feeds/%s_description.html' % slug) > > > > > > > 第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学. > > > > > 这种用法体现了python的动态特性。在处理self.attribute时, > 如果要处理的是一个实例,那么python先在实例的属性中查找是否有attribute这个属性,如果没有,则查找对应的类属性是否有,如果还有没有,再按基类顺序来查找基类中的这个属性,如果还有没,则处理特殊的类方法,如__getattr__或__getattribute__(用在newstyleclass中),再没有就抛异常了。 > > > > 所以self.attribute当实例属性不存在时会自动引用类属性。 > > > > -- > > I like python! > > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > > My Blog: http://www.donews.net/limodou > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -- > 蔡峰 Cai Feng > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061107/6dd44f87/attachment.html
2006年11月07日 星期二 01:12
很抱歉,文字表达有误,重发一遍。 在javascript的例子中,a应当是一个对象初创化函数,或者该函数本身可以赋值给其它对象的属性。 那么this.attribute=1中的this表示它创建的对象或依附对象,该表达式将该对象的attribute属性赋值为1。比如 oA=new a();//创建的对象 或者 oB.fn=a;//依附一个对象上 oB.fn(); 上面执行后,oA.attribute和oB.attribute的值就成为1。 至于attribute2=2,则仅仅是对attribute2进行赋值,这个attribute2 只是个变量,与上述的对象没有什么关系。注意到你给的例子没有var声明,该变量应当是该函数外的变量(全局变量,或包含a函数的域的局部变量)。 如果有函数内部用var进行了声明,声明的变量就是该函数的局部对象,不要将它看成java类的域成员,在javascript中,这个变量仅仅是一个函数的局部对象而已,即使这个函数用来创建类对象。 On 11/7/06, fdcn <fdcn64在gmail.com> wrote: > > 在javascript的例子中,a应当是一个对象初创化函数,或者是一个可以赋值给其它对象的属性。 > 那么this.attribute=1中的this表示它创建的对象或依附对象,表达式将该对象的attribute属性赋值为1。比如 > oA=new a()或者oB.fn=a;oB.fn(); > 执行后,oA.attribute和oB.attribute的值就成为1。 > > 至于attribute2=2,则仅仅是对attribute进行赋值,这个attribute只是个变量,与上述的对象没有什么关系。注意到你给的例子没有var声明,该变量应当是该函数外的变量(全局变量,或包含a函数的域的局部变量)。 > > > On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > > > > 呵呵,解释的很详细,谢谢. > > 我把类里面的attribute当成java中的staic属性的内容了. > > 把__init__中的self.attribute当成java中的一般属性了. > > > > 另外我还有一个疑问, > > 在javascript中也有 > > function a(){ > > this.attribute=1; > > attribute=2; > > } > > 这两个attribute使用时我也不太明白,不知道能否解释一下? > > > > On 11/6/06, limodou <limodou在gmail.com> wrote: > > > On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > > > > class Feed(object): > > > > > > item_pubdate = None > > > > > > item_enclosure_url = None > > > > > > feed_type = feedgenerator.DefaultFeed > > > > > > title_template = None > > > > > > description_template = None > > > > > > > > > > > > def __init__(self, slug, feed_url): > > > > > > self.slug = slug > > > > > > self.feed_url = feed_url > > > > > > self.title_template_name = self.title_template or > > ('feeds/%s_title.html' > > > > > > % slug) > > > > > > self.description_template_name = self.description_template or > > > > > > ('feeds/%s_description.html' % slug) > > > > > > > > > > 第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学. > > > > > > > 这种用法体现了python的动态特性。在处理self.attribute时,如果要处理的是一个实例,那么python先在实例的属性中查找是否有attribute这个属性,如果没有,则查找对应的类属性是否有,如果还有没有,再按基类顺序来查找基类中的这个属性,如果还有没,则处理特殊的类方法,如__getattr__或__getattribute__(用在newstyleclass中),再没有就抛异常了。 > > > > > > > > 所以self.attribute当实例属性不存在时会自动引用类属性。 > > > > > > -- > > > I like python! > > > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > > > > > My Blog: http://www.donews.net/limodou > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > > 蔡峰 Cai Feng > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061107/ccbf502d/attachment.html
2006年11月07日 星期二 01:18
谢谢fdcn <fdcn64在gmail.com> 的回答. 很不好意思,我代码打错了. 第二个attribute也应该是a这个类中的元素,我分不清楚,这里的两个attribute有什么区别,但是我知道在使用过程中肯定是有区别的.因为我在实际编码过程中遇到过类似的问题,以前都是直接跳过去的,并没有深究.我觉得这里应该是我对变量作用域理解不够造成的. 代码如下: function a(){ this.attribute=1; var attribute=2; function innerfunc(){ alert(attribute); } function innerfunc2(){ alert(this.attribute); } } test1=new a(); test1.innerfunc(); test1.innerfunc2(); test1.attribute=3; test2=new a(); test2.innerfunc(); test2.innerfunc2(); 其实主要就是a类中的那个var attribute是不是在所有a的对象中只有一份,也就是跟static类似.a中的this.attribute是挺容易理解的. On 11/7/06, fdcn <fdcn64在gmail.com> wrote: > 在javascript的例子中,a应当是一个对象初创化函数,或者是一个可以赋值给其它对象的属性。 > 那么this.attribute=1中的this表示它创建的对象或依附对象,表达式将该对象的attribute属性赋值为1。比如 > oA=new a()或者oB.fn=a;oB.fn(); > 执行后,oA.attribute和oB.attribute的值就成为1。 > > 至于attribute2=2,则仅仅是对attribute进行赋值,这个attribute只是个变量,与上述的对象没有什么关系。注意到你给的例子没有var声明,该变量应当是该函数外的变量(全局变量,或包含a函数的域的局部变量)。 > > > On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > > > > 呵呵,解释的很详细,谢谢. > > 我把类里面的attribute当成java中的staic属性的内容了. > > 把__init__中的self.attribute当成java中的一般属性了. > > > > 另外我还有一个疑问, > > 在javascript中也有 > > function a(){ > > this.attribute=1; > > attribute=2; > > } > > 这两个attribute使用时我也不太明白,不知道能否解释一下? > > > > On 11/6/06, limodou <limodou在gmail.com> wrote: > > > On 11/6/06, cai feng <caifen1985在gmail.com> wrote: > > > > class Feed(object): > > > > > > item_pubdate = None > > > > > > item_enclosure_url = None > > > > > > feed_type = feedgenerator.DefaultFeed > > > > > > title_template = None > > > > > > description_template = None > > > > > > > > > > > > def __init__(self, slug, feed_url): > > > > > > self.slug = slug > > > > > > self.feed_url = feed_url > > > > > > self.title_template_name = self.title_template or > ('feeds/%s_title.html' > > > > > > % slug) > > > > > > self.description_template_name = self.description_template or > > > > > > ('feeds/%s_description.html' % slug) > > > > > > > > > 第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学. > > > > > > > > 这种用法体现了python的动态特性。在处理self.attribute时,如果要处理的是一个实例,那么python先在实例的属性中查找是否有attribute这个属性,如果没有,则查找对应的类属性是否有,如果还有没有,再按基类顺序来查找基类中的这个属性,如果还有没,则处理特殊的类方法,如__getattr__或__getattribute__(用在newstyleclass中),再没有就抛异常了。 > > > > > > 所以self.attribute当实例属性不存在时会自动引用类属性。 > > > > > > -- > > > I like python! > > > UliPad <>: > http://wiki.woodpecker.org.cn/moin/UliPad > > > My Blog: http://www.donews.net/limodou > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > > 蔡峰 Cai Feng > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > -- 蔡峰 Cai Feng
2006年11月07日 星期二 03:05
其实我在上面已经说了它们的区别了,区别就在于,this.attr的attr是该对象的一个属性,var attr只是这个函数的一个局部变量。
我们简单点,看一看你的例子在功能和概念上与java是怎么对应的:
javascript:
function a(){
this.attribute=1;
var attribute=2;
}
java:
class a{
public int attribute=1;
public a(){
int attribute=2;
}
}
注意到了吗?虽然它们都用同样的标识符,但两个变量是完全不同的。javascript的a函数的作用就是java中a类的构造函数,所有在javascript的a函数中定义的变量只是OOP中构造函数的局
部变量。由于javascript是动态语言,它不需要明确定义类(在js中实际是对象)的域变量,只需要在函数中直接使用this.attribute即可,javascript就会动态地为该对象增加一个"域变量"(实际上是属性)。
随着函数执行完毕,这个var声明的局部变量就被垃圾回收了,对,没有了!(除非还有变量指向该局部对象,涉及闭包函数,暂不讨论)
局部变量的作用在javascript函数的大多数情况下就是一临时变量。
比如:
function a(){
var m=8;
}
x=new a();
这里,m已完全没有了,完全不可访问的,因为它只存在于new a()这一短暂的时间中,现在你想通过x.m是不可能访问到的。这个x与new
Object()产生的对象本质上是一样的,就象m从来没有出现过一样。请回想c或java的函数局部变量。
但this.attr不同,它是对象的属性,相当于c++或java的域变量,可以通过点操作符来访问的。
其实,javascript开发者常常利用局部变量这一点来实现"私有"域变量,需要运用闭包的技巧,暂不叙述。
回过头来,你的例子下方的测试是有错误的,由于内部变量和函数都是临时的(局部的),根本不可能从对象处用点操作符访问,就象不能访问java类a的构造函数里的attribute一样:
test1=new a();
test1.innerfunc();//出错:test1没有innerfunc函数
test1.innerfunc2();//出错:test1没有innerfunc2函数
test1.attribute=3;//正确,它把构造函数中this.attribute的值由1重新赋值为3
test2=new a();
test2.innerfunc();//出错:test2没有innerfunc函数
test2.innerfunc2();//出错:test2没有innerfunc2函数
On 11/7/06, cai feng <caifen1985在gmail.com> wrote:
>
> 谢谢fdcn <fdcn64在gmail.com> 的回答.
> 很不好意思,我代码打错了.
> 第二个attribute也应该是a这个类中的元素,我分不清楚,这里的两个attribute有什么区别,
> 但是我知道在使用过程中肯定是有区别的.因为我在实际编码过程中遇到过类似的问题,以前都是直接跳过去的,
> 并没有深究.我觉得这里应该是我对变量作用域理解不够造成的.
> 代码如下:
> function a(){
> this.attribute=1;
> var attribute=2;
> function innerfunc(){
> alert(attribute);
> }
> function innerfunc2(){
> alert(this.attribute);
> }
> }
>
> test1=new a();
> test1.innerfunc();
> test1.innerfunc2();
> test1.attribute=3;
> test2=new a();
> test2.innerfunc();
> test2.innerfunc2();
>
> 其实主要就是a类中的那个var attribute是不是在所有a的对象中只有一份,
> 也就是跟static类似.a中的this.attribute是挺容易理解的.
>
>
> On 11/7/06, fdcn <fdcn64在gmail.com> wrote:
> > 在javascript的例子中,a应当是一个对象初创化函数,或者是一个可以赋值给其它对象的属性。
> > 那么this.attribute=1中的this表示它创建的对象或依附对象,表达式将该对象的attribute属性赋值为1。比如
> > oA=new a()或者oB.fn=a;oB.fn();
> > 执行后,oA.attribute和oB.attribute的值就成为1。
> >
> >
> 至于attribute2=2,则仅仅是对attribute进行赋值,这个attribute只是个变量,与上述的对象没有什么关系。注意到你给的例子没有var声明,该变量应当是该函数外的变量(全局变量,或包含a函数的域的局部变量)。
> >
> >
> > On 11/6/06, cai feng <caifen1985在gmail.com> wrote:
> > >
> > > 呵呵,解释的很详细,谢谢.
> > > 我把类里面的attribute当成java中的staic属性的内容了.
> > > 把__init__中的self.attribute当成java中的一般属性了.
> > >
> > > 另外我还有一个疑问,
> > > 在javascript中也有
> > > function a(){
> > > this.attribute=1;
> > > attribute=2;
> > > }
> > > 这两个attribute使用时我也不太明白,不知道能否解释一下?
> > >
> > > On 11/6/06, limodou <limodou在gmail.com> wrote:
> > > > On 11/6/06, cai feng <caifen1985在gmail.com> wrote:
> > > > > class Feed(object):
> > > > > > > item_pubdate = None
> > > > > > > item_enclosure_url = None
> > > > > > > feed_type = feedgenerator.DefaultFeed
> > > > > > > title_template = None
> > > > > > > description_template = None
> > > > > > >
> > > > > > > def __init__(self, slug, feed_url):
> > > > > > > self.slug = slug
> > > > > > > self.feed_url = feed_url
> > > > > > > self.title_template_name = self.title_template or
> > ('feeds/%s_title.html'
> > > > > > > % slug)
> > > > > > > self.description_template_name = self.description_template or
> > > > > > > ('feeds/%s_description.html' % slug)
> > > > >
> > > > >
> >
> 第一个title_template与__init__中的title_template是一个东西吗?我怎么觉得第一个是Feed类的,第二个是Feed对象拥有的?不知道我的理解对不对,初学.
> > > > >
> > > >
> > 这种用法体现了python的动态特性。在处理self.attribute时,
> 如果要处理的是一个实例,那么python先在实例的属性中查找是否有attribute这个属性,如果没有,则查找对应的类属性是否有,如果还有没有,再按基类顺序来查找基类中的这个属性,如果还有没,则处理特殊的类方法,如__getattr__或__getattribute__(用在newstyleclass中),再没有就抛异常了。
> > > >
> > > > 所以self.attribute当实例属性不存在时会自动引用类属性。
> > > >
> > > > --
> > > > I like python!
> > > > UliPad <>:
> > http://wiki.woodpecker.org.cn/moin/UliPad
> > > > My Blog: http://www.donews.net/limodou
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > > --
> > > 蔡峰 Cai Feng
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> --
> 蔡峰 Cai Feng
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061107/856de6d3/attachment.htm
2006年11月07日 星期二 03:43
> > 其实主要就是a类中的那个var attribute是不是在所有a的对象中只有一份, > 也就是跟static类似.a中的this.attribute是挺容易理解的. 这个var attribute是随着每一次new a()时新生成,new a()结束后又自动销毁,只剩下new出来的新对象了。你认为会只有一份吗?在我们的例子中,一份也没有,它产生新对象后就消失了。 不过,我们应当把它看成随着每一个对象都有一份拷贝。因为在有此情况下,这个局部变量并不消失,这时我们看到的就是每产生一个新对象就有一个新的var attribute产生。 你理解的static变量,在js中并没有完全对应的东西,但可以通过每一个对象的prototype属性实现类似的只有一份的属性(主要是函数)。但抱歉,它不是static。 建议你看看《javascript权威指南》或《javaScript高级程序设计》等书,这里是python的列表,我不好篇幅长了。 -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061107/2143f80b/attachment.html
2006年11月07日 星期二 13:18
On 11/7/06, fdcn <fdcn64在gmail.com> wrote: > > > 其实主要就是a类中的那个var > attribute是不是在所有a的对象中只有一份,也就是跟static类似.a中的this.attribute是挺容易理解的. > > > 这个var attribute是随着每一次new a()时新生成,new > a()结束后又自动销毁,只剩下new出来的新对象了。你认为会只有一份吗?在我们的例子中,一份也没有,它产生新对象后就消失了。 > > 不过,我们应当把它看成随着每一个对象都有一份拷贝。因为在有此情况下,这个局部变量并不消失,这时我们看到的就是每产生一个新对象就有一个新的var > attribute产生。 > > 你理解的static变量,在js中并没有完全对应的东西,但可以通过每一个对象的prototype属性实现类似的只有一份的属性(主要是函数)。但抱歉,它不是static。 > > 建议你看看《javascript权威指南》或《javaScript高级程序设计》等书,这里是python的列表,我不好篇幅长了。 > this.attribute是一个实例属性,因此可以被外部进行使用。而var attribute是一个局部变量,不能为外部进行使用。 记得好象有人通过局部变量来实现私有属性。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年11月09日 星期四 00:42
非常感谢fdcn<fdcn64在gmail.com>的回答 你的回答已经很详细了,我已经清楚这里面的区别了. 我觉得你提到的function a(){}是构造函数这一点对我理解的帮助最大了. 很抱歉,在python邮件列表中询问了这么多javascript问题.我想对于我这个讨论就到这里结束吧,其他问题我会通过相关途径自己解决掉的. 再次感谢大家关注. On 11/7/06, fdcn <fdcn64在gmail.com> wrote: > > > 其实主要就是a类中的那个var > attribute是不是在所有a的对象中只有一份,也就是跟static类似.a中的this.attribute是挺容易理解的. > > 这个var attribute是随着每一次new a()时新生成,new > a()结束后又自动销毁,只剩下new出来的新对象了。你认为会只有一份吗?在我们的例子中,一份也没有,它产生新对象后就消失了。 > > 不过,我们应当把它看成随着每一个对象都有一份拷贝。因为在有此情况下,这个局部变量并不消失,这时我们看到的就是每产生一个新对象就有一个新的var > attribute产生。 > > 你理解的static变量,在js中并没有完全对应的东西,但可以通过每一个对象的prototype属性实现类似的只有一份的属性(主要是函数)。但抱歉,它不是static。 > > 建议你看看《javascript权威指南》或《javaScript高级程序设计》等书,这里是python的列表,我不好篇幅长了。 > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > -- 蔡峰 Cai Feng
Zeuux © 2025
京ICP备05028076号