Reputation: 5589
Hi as far as I see in Python variables are untyped. So now I want to have a baseclass
class baseClass:
def x():
print "yay"
and two subClasses
class sub1(baseClass):
def x():
print "sub1"
class sub2(baseClass):
def x():
print "sub2"
in other programming languages I can develop against interfaces just like
baseClass c = new sub1()
so now I can use c as a baseClass with the functionality of sub1 and maybe at runtime I can change it via
c = new sub2()
Is that possible in python as well?
Upvotes: 1
Views: 594
Reputation: 76715
Python doesn't really have "variables". What Python has are names that have objects bound to them.
class X(object):
pass
class Y(object):
pass
class Z(object):
pass
a = X # name "a" bound with class object "X"
print(a is X) # prints True
a = Y
print(a is Y) # prints True
b = Y
print(a is b) # prints True
The above code shows binding the name "a" with first one object (the class object defined as "X") and then another object (class object "Y"). Then we bind "b" to class object "Y". "a" and "b" are then referring to the same object, so the is
test returns True
.
Python is strongly typed. As long as "a" is bound with the class object "Y", the type of "a" is the type of the class object. For example, if you try to use "a" in a math expression, Python would raise a TypeError
exception, because "a" would be the wrong class.
print(a + 3) # causes TypeError exception
But it is always legal to rebind a name to point to some other object that can have some other type.
a = 3
print(a + 3) # prints 6
A good explanation is here: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names
Upvotes: 1
Reputation: 16977
Yes.
c = sub1()
c = sub2()
But the base class and concept of defining an interface are unnecessary, since python is not statically typed.
EDIT:
To rewrite your code in valid Python:
# This space where baseClass was defined intentionally left blank,
# because it serves no purpose
class Sub1(object):
def x(self):
print "sub1"
class Sub2(object):
def x(self):
print "sub2"
c = Sub1()
c = Sub2()
Upvotes: 5
Reputation: 22603
Yes, you can do this; however, the syntax to instantiate objects is not "new Class(args)". Simply drop the "new".
Taking a step back, assume you have an object stored in a variable named foo. The compiler will never complain about you doing the following:
foo.bar
This is true even if there's no way foo can have the property bar. This is because whether foo has an attribute named bar is determined at run time (this is why Python is a dynamically typed language). If your code has a type problem, you might not find out about it until you actually run it (i.e. runtime).
In Python, interfaces are established purely by convention among developers of the same project. This method of dealing with interfaces is known as duck typing. I suggest you read up on this subject, since it seems you are new to Python.
PS: Dynamic typing might sound like a bad thing because you don't get compile-time type checking, but the flip side of this is that there is much less boiler plate that you have to write. Theoretically, this makes you much more productive. In my experience, this tends to bear out.
Upvotes: 2