1

I've been using AS3 before, and liked its grammar, such as the keyword as.

For example, if I type (cat as Animal) and press . in an editor, the editor will be smart enough to offer me code hinting for the class Animal no matter what type cat actually is, and the code describes itself well.

Python is a beautiful language. How am I suppose to do the above in Python?

Pinyin
  • 586
  • 4
  • 19
  • possible duplicate of [How to cast object in Python](http://stackoverflow.com/questions/9112300/how-to-cast-object-in-python) –  Jul 14 '13 at 07:49
  • 4
    Python doesn't have strict typing of variables, so there's no way to express that a given variable has a given type. Some IDEs might be clever enough to deduce the type of an object if there's only one possibility, but there's nothing in the language to explicitly support that (and many situations where it won't be possible). – Blckknght Jul 14 '13 at 07:49
  • You might also like to take a look at iPython and/or at some IDEs like wing that give you context completion. – Steve Barnes Jul 14 '13 at 07:51
  • @Blckknght Thanks, I was able to find this answer [link](http://stackoverflow.com/a/6874794/1815491) after your description. Good enough for me. – Pinyin Jul 14 '13 at 08:27

6 Answers6

6

If you were to translate this literally, you'd end up with

cat if isinstance(cat, Animal) else None

However, that's not a common idiom.

How am I suppose to do the above in Python?

I'd say you're not supposed to do this in Python in general, especially not for documentation purposes.

phant0m
  • 16,595
  • 5
  • 50
  • 82
0

You are looking for a way to inspect the class of an instance.

isinstance(instance, class) is a good choice. It tells you whether the instance is of a class or is an instance of a subclass of the class.

In other way, you can use instance.__class__ to see the exact class the instance is and class.__bases__ to see the superclasses of the class.

For built-in types like generator or function, you can use inspect module.

Names in python do not get a type. So there is no need to do the cast and in return determine the type of an instance is a good choice.

As for the hints feature, it is a feature of editors or IDEs, not Python.

zhangyangyu
  • 8,520
  • 2
  • 33
  • 43
  • 1
    This is either a tongue in cheek comment, or nowhere near equivalent to what the poster wants in an answer. – phant0m Jul 14 '13 at 08:17
  • I misunderstand the meaning of the OP. Now I have updated my answer. @phant0m – zhangyangyu Jul 14 '13 at 09:06
  • @zhangyangyu I don't know AS3, but Googling the `as` operator reveals that it's a casting operator; `(cat as Animal)` in AS3 is roughly equivalent, I think, to `(Animal)cat` in C, C++ or Objective-C. This still isn't quite what you've answered. – Mark Amery Jul 14 '13 at 09:13
  • In Python, the variables has no type. So to the most extend, the feature he wants is not needed. And in return, I think inspect the type of a class is the best choice.@MarkAmery – zhangyangyu Jul 14 '13 at 09:27
  • Traditionally, you get type of an object with `type(cat)` though, not by accessing `__class__`. – phant0m Jul 14 '13 at 10:38
0

You're looking for polymorphic behavior in Python.

It doesn't really work the same way as in other languages like Java - what we have is isinstance().

Which leads to

class FirstObject(object):
    #someStuff

class SecondObject(FirstObject):
    #someStuff

>> someObject = SecondObject()
>> isinstance(someObject, SecondObject)
>> true

and the same for

>> isinstance(someObject, FirstObject)
>> true

How this works then with the dot notation is something called the Method Resolution Order, or MRO where it looks up methods "left to right" in the class declaration. This feature isn't available in the shell ATM but is scheduled for an upcoming release of Python.

When writing this I found this SO question - rendering my answer moot.

Community
  • 1
  • 1
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
0

In Python, object is created to be an instance of a class, you cannot declare an existing object to be an instance of a class. Since the class have __init__ method, the initialization is essence for a class instance.

mayaa
  • 173
  • 1
  • 2
  • 10
0

Python has no type declarations. If cat is an Animal, you can just use it as an Animal, and it'll work. You don't have to cast it to an expression of type Animal, like you would in a statically typed language. Unfortunately, this also means it's hard to provide code completion, since for all your IDE knows, cat could be a Nacho.

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

You can, in some circumstances, change the class of an existing instance of an object by setting its __class__ attribute:

class Foo(object):
    @property
    def myname(self):
        return "I'm a Foo"

class Bar(object):
    @property
    def myname(self):
        return "I'm a Bar"


thing = Foo()

print thing.myname

thing.__class__ = Bar

print thing.myname

I think this only works for members of "new-style" Python classes derived from object.

ali_m
  • 71,714
  • 23
  • 223
  • 298