Is Python strongly typed?

后端 未结 11 1361
长发绾君心
长发绾君心 2020-11-22 17:12

I\'ve come across links that say Python is a strongly typed language.

However, I thought in strongly typed languages you couldn\'t do this:

bob = 1
b         


        
11条回答
  •  -上瘾入骨i
    2020-11-22 17:21

    class testme(object):
        ''' A test object '''
        def __init__(self):
            self.y = 0
    
    def f(aTestMe1, aTestMe2):
        return aTestMe1.y + aTestMe2.y
    
    
    
    
    c = testme            #get a variable to the class
    c.x = 10              #add an attribute x inital value 10
    c.y = 4               #change the default attribute value of y to 4
    
    t = testme()          # declare t to be an instance object of testme
    r = testme()          # declare r to be an instance object of testme
    
    t.y = 6               # set t.y to a number
    r.y = 7               # set r.y to a number
    
    print(f(r,t))         # call function designed to operate on testme objects
    
    r.y = "I am r.y"      # redefine r.y to be a string
    
    print(f(r,t))         #POW!!!!  not good....
    

    The above would create a nightmare of unmaintainable code in a large system over a long period time. Call it what you want, but the ability to "dynamically" change a variables type is just a bad idea...

提交回复
热议问题