object declaration and initialization for IDE autocompletion

The problem with python is that an object can be associated to ‘None’ class before it is associated on another class, but if it is associated to a specific class, it can’t be null.

class FooOne(object):
    ft=None
    def __init__(self,m):
        self.ft=m

class FooTwo(object):pass

f2=FooTwo 
f1=FooOne(f2)

I can do this, but the problem, from now on, is that no IDE is able to suggest me in autocompletion, because ft can be any of the classes passed as parameter, but this will be clear only at execution time.

Can i specify that an item of a class will be of a specified type, and if i not initialize it, it remains with something like null ? I try to explain : something like :

class FooOne(object):
    FooTwo ft
    def __init(self,m):
        self.ft=m

...

The problem is that a well structured program will never give relief by autocompletions of IDE, if we can write the source specifing the type of object to store on classes at compilation time.

mizar auto completion is for wussies!

Many languages like java and c++ require auto completion because they are so verbose. Python which is nice and concise does not really need that. Large java and c++ programs require many many people to write while large python program can be hacked together by very small amount of people that know whats going on.

The only exception is libraries. But python IDE’s normally uses different algorithm to auto complete those then user files.

First you have to distinguish between three fundamental things in programming: class, instance, variable. You seem to mix instance and variable together in what you call ‘item’ or ‘object’. This is dangerous, both in weak-typed languages like Python or Perl, and in strong-typed languages like C++ or Java.

An instance has one type, and in Python this type will never change from the point when the instance is created (allocated) to the point when it is destroyed (deallocated). In Perl this is possible, by ‘blessing’ instances.

An instance can be assigned to a variable. But at a later point of time another instance can be assigend to the same variable (Comment: ‘simple’ things like integers or strings are instances too, e.g. 7 or ‘xyz’).

Because you can assign different instances to a variable is not possible to make a statement about the type of an variable in a weak-typed langages (in any weak typed language). The best you can do is to say what type is assigned to a variable at a particular point of time. I use ‘point of time’ because it would not be true for ‘line of code’. See:

>>> x = 7 # <-- create an instance of class 'int', and assign it to the variable 'x'
>>> type( x )
<type 'int'>
>>> x = 'abc' # <-- create an instance of class 'string', and assign it to the variable 'x'
>>> type( x )
<type 'str'>
>>> x = object() # <-- create an instance of class 'object', and assign it to the variable 'x'
>>> type( x )
<type 'object'>

To show that ‘line of code’ would be wrong:

>>> x = None
>>>
>>> def foo( value ):
...   global x # <-- it is not possible to say what type( x ) is at this line of code
...   x = value
...
>>> foo( 7 )
>>> type( x )
<type 'int'>
>>> foo( 'abc' )
>>> type( x )
<type 'str'>

Let’s have a look at your words, and how they would be worded right:

An instance of the class ‘None’ can be assigned to a variable. ‘None’ is a normal class like any other too, and it has instances!

>>> x = type( None )
>>> x
<type 'NoneType'>
>>> type( x )
<type 'type'>

You can assign an instance of class ‘None’ to a variable, but later you can assign an instance of another class to the same variable.

If a variable is assign an instance of a class other than ‘None’, then it has not assigned an instance at of class ‘None’ at the same time. Obvious now.

I hope this sheds some light on how Python and other weak-typed languages work. Being weak-typed is a great advantage, but it does have disadvantages too. for example that an IDE can’t always figure out what type of instance is assigned to a variable at a certain position in the code.

enn0x

this, is true. you say c++ and java require auto-completion because they are verbose. that is very diplomatic of you. i’d say it’s because they require a silly amount of mindless finger typing, especially java.

as for auto-completion to help out with using libraries, really, it doesn’t so much. you can use auto-completion to kind of sort of guess at what something does by it’s name, but to really know, you still need to rtfm. so just rtfm, and don’t worry about auto-completion.

my view is that auto-completion is kinda sorta convenient. i can do without though.

(Note that the following comes from one who learned C++ before Python.)

Personally, I’m rather a fan of auto-completion, and find the difficulty that languages like Python present to its use to be a little annoying.

While hardly a terribly important feature, I’ve in the past found it to be very convenient, allowing one to find a poorly-remembered method name, or browse through the available methods (presuming that they are named reasonably well and one can thus at least guess at some of the useful ones) without switching out to a manual, let alone taking the time to search one. (Naturally, the latter can provide more detailed information if sufficiently filled-out, but if one already has a good idea of what is wanted, or just wants to browse the available methods quickly, I think that I find auto-completion to be a preferable means.)

I also disagree with the “mindless finger-typing” charge, especially given that Python seems to want the “self.” prefix before member-names, even within the class itself.

Please don’t misunderstand me - Python has some great advantages (such as not calling for compilation between a source change and a program run), and has some rather enjoyable features, once you get to know it, so to speak. I just think that both language types have their advantages, and am rather fond of auto-completion.

I do miss strong typing, and the advantages that it confers, however. :confused:

Technically, auto-completion is a function of an IDE, not the language. There are IDE’s that will provide this feature for Python.

Hi all. I begin from the last that replied to my thread.

@Kijaro
Yes, is an IDE feature, and is exactly the problem i’m speaking about, and i’m telling that no IDE is capable to detect the type, because the language is far more flexible than java, so types are not declared necessarily at compilation time.

@All
I know that python, perl, php have real need to allow fast writing, smaller modules, and small verbosity, however, as an experienced java enterprise programmer, i must say that this verbose mode of writing lead to very less problems in resolving issues with class instantiation.
I’m understing very well the terms you are explaining me (maybe my english is not so good), and i’m aquainted with object oriented programming.

Sorry, i’m trying only to know if it is a method to declare a class type before the instantiation of a non null object. There should be an option for a programmer to specify types at compilation time, not a python modification, but a programming option.

Thanks to all for the answers. After all i really like panda and python, and i hope that larger projects will not lead me to more and more logical errors caused by types not defined.

There are some python type checkers out there. They do not check at compile time, but at run time. But they are more powerful than say the one from Java e.g. you can define a type color that need three flaots, but in a specific range. It is more like Design By Contract. Zope is using one AFAIK. The bad thing is, i do not know if there is any IDE that is capable to use this information. One more bad thing is that there are to many typecheckers but AFAIK the Python community tries to standardize this with a PEP.

One more thing. You do not have to write self if you really hate that, it is not a keyword, you can write whatever you want. Write s if you like, but don’t get angry if other programmers start to blame you :slight_smile: and don’t say to anyone that I’ve said this to you.

One more reason why self is a good idea. It supports locality. While writing a Java program it may be possible that the class spans two screens. If I don’t use some prefixes (if you do, then it is like python) and have to read and analyze the whole method to understand which variables are local and which are not (given you post this class somewhere on the web like the tons of python snippets here).

allready done in python 3000 (in bata now soon to be come the new standard)

you can write, defining the type at the end.

def normpathƛ(path✎)✎:
    """Normalize path, eliminating double slashes, etc."""
    if path✎ == '':
        return '.'
    initial_slashes✓ = path✎.startswithƛ('/')✓
    # POSIX allows one or two initial slashes, but treats three or more
    # as single slash.
    if (initial_slashes✓ and
        path✎.startswithƛ('//')✓ and not path✎.startswithƛ('///')✓)✓:
        initial_slashesâ„• = 2
    comps♨ = path✎.splitƛ('/')♨
    new_comps♨ = []♨
    for comp✎ in comps♨:
        if comp✎ in ('', '.')⒯:
            continue
        if (comp✎ != '..' or (not initial_slashesℕ and not new_comps♨)✓ or
             (new_comps♨ and new_comps♨[-1]✎ == '..')✓)✓:
            new_comps♨.appendƛ(comp✎)
        elif new_comps♨:
            new_comps♨.popƛ()✎
    comps♨ = new_comps♨
    path✎ = '/'.join(comps♨)✎
    if initial_slashesâ„•:
        path✎ = '/'*initial_slashesℕ + path✎
    return path✎ or '.'

python.org/dev/peps/pep-3117/

or python.org/dev/peps/pep-3107/


def haul(item: Haulable, *vargs: PackAnimal) -> Distance:
    ...

maybe mindless is a little harsh, but here’s an example:

//in java
MessageDigest md = MessageDigest.getInstance(“SHA-1”);

#in python
md = hashlib.sha1()

see what i’m saying?

and since not everything is a reference in java (just almost everything), you have to do this:

ClassName c = new ClassName();

now, in c++, this is meaningful (imagine that’s a pointer declaration). but since all objects have to be allocated on the heap in java, why is there even an operator new. oh, that’s right, there’s a few things you can allocate on the stack. just not classes.

these things are minor gripes, but they just annoy the shit out of me.

also, although this isn’t required, i always use it, so self doesn’t bother me. but that’s taste for you.

self bother me a lot till i learned my lesson.

I was programming c++, and had a var declared in class scope and function scope and the program would segfault it took me like a day to figure out what it is … a day i never would have to spend if i used “this” or the panda3d conventions of any member vars _var … self.var is not as good as some proposal like .var but i think some thing should stay to show what you are talking about and now we are kind of attached to self for sentimental reasons.

Another hidden thing about self - if you have a lot of self in your code it looks a little bad … it also means that you are carrying to much state in your classes (and should be broken up) so it also kind of a “code rot meter” for me.

Minor nitpick, Python is not a weakly typed language, it uses strong dynamic typing.

Strong typing refers to instances, and Python is strong because every instance is strictly typed, with no automatic coersion. If you supply a string as a parameter to a function requiring a float, an error will occur. This is unlike Perl, and to a limited extend C/C++ which can do some automatic type conversions (and can be a source of errors if those conversions do not go as expected).

Dynamic typing means that each variable can take on different types, determined at run time.