469,271 Members | 1,787 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

class method question

hello
can you explain why python does not see difference between instance
method and class method, having the same name

example
>>class toto(object):
.... def f(self):
.... print('instance method')
.... @classmethod
.... def f(cls):
.... print('class method')
....
>>t=toto()
t.f
<bound method type.f of <class '__main__.toto'>>
>>t.f()
class method
>>toto.f()
class method
if i do the same in ruby:
"""
class Toto
def meth
print "instance method\n"
end

def Toto.meth
print "class method\n"
end
end

i = Toto.new
i.meth
Toto.meth
"""

$ruby1.9 test_classmethod.rb
instance method
class method

thanks
sylvain
Oct 25 '06 #1
2 1243
Sylvain Ferriol wrote:
can you explain why python does not see difference between instance
method and class method, having the same name
You're trying to put two objects with the same name in the same namespace.
example
>>class toto(object):
... def f(self):
... print('instance method')
... @classmethod
... def f(cls):
... print('class method')
...
>>t=toto()
>>t.f
<bound method type.f of <class '__main__.toto'>>
>>t.f()
class method
>>toto.f()
class method

if i do the same in ruby:
Python works better if you use it to write Python programs.

</F>

Oct 25 '06 #2
Sylvain Ferriol wrote:
hello
can you explain why python does not see difference between instance
method and class method, having the same name
Because class namespaces are dicts, and you can't have duplicate keys in
dicts. It's totally unrelated to class/instance method distinction (try
with any kind of attribute, you'll get the same result : only the last
attribute is kept).
example
>>>class toto(object):
... def f(self):
... print('instance method')

... @classmethod
... def f(cls):
... print('class method')
>>>t=toto()
t.f
<bound method type.f of <class '__main__.toto'>>
>>>t.f()
class method
>>>toto.f()
class method
Also note that Python classmethods can be called on instances. Here
you're calling the same function twice.

A python object is a data structure holding references to other objects.
One of these references points to a dict (named __dict__) storing the
object's own attributes ('instance' attributes), and another one (named
__class__) points to the class object - which is itself an object that
holds references to it's parent classes.

When a name is looked up on an object, the name is first looked up in
the object's __dict__, then in the class, then in the class's parent
classes...

If you want a Ruby-like behaviour, it's still possible, but you have to
do it by manually:

import types

class Rubysh(object):
@classmethod
def test(cls):
print "in classmethod test, cls : %s" % cls

def __init__(self):
def test(self):
print "in instance method test, self : %s" % self
self.test = types.MethodType(test, self, self.__class__)

Rubysh.test()
r = Rubysh()
r.test()

Also note that most python programmers may find this a bit confusing,
since they'd expect Rubish.test() and r.test() to resolve to the same
attribute...
>
if i do the same in ruby:
You're not "doing the same", you're doing something that *looks* the same.

While they look quite similar at first sight - and indeed share a lot of
characteristics (lightweight, highly dynamic, hi-level object languages)
-, Python and Ruby are very different beasts under the hood. So beware :
superficial similarities can hide very different meanings and behaviours.

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Kerry Neilson | last post: by
2 posts views Thread by He Shiming | last post: by
6 posts views Thread by roland.bali | last post: by
5 posts views Thread by Ben | last post: by
1 post views Thread by =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.