By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,171 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,171 IT Pros & Developers. It's quick & easy.

class method question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.