472,334 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

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 1322
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry...
2
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public:...
9
by: Oriane | last post by:
Hi, I'm currently a method attribute which is used to check the "validity" of this method against a rule. I wrote the isValid method, to be used...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load....
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic...
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable...
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a...
1
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
In the following code, calling CallTestMethod() from an instance of the derived class (the base is abstract and we can never instantiate it),...
2
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.