简体   繁体   中英

Can someone help me on why this python script won't run?

import numbers

class base62(numbers.Number):
    digits='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    def __init__(self,value):
        if isinstance(value,int):
            self.value=value
            if value==0:
                self.string='0'
            else:
                self.string='' if value>0 else '-'
                while value!=0:
                    value,d=divmod(value,62)
                    self.string=self.digits[d]+self.string
        elif isinstance(value,(str,bytes)):
            assert(value.isalnum())
            self.string=str(value)
            self.value=0
            for d in value:
                self.value=self.value*62+self.digits.index(d)
    def __int__(self):
        return self.value
    def __str__(self):
        return self.string
    def __repr__(self):
        return self.string


Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import base62
>>> b = base62(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> 

base62 appears to be the name of both your module and your function inside that module. Try:

>>> import base62
>>> b = base62.base62(3)

Read the error message:

TypeError: 'module' object is not callable

TypeError means there was an Error because something was of the wrong Type . The thing in question is base62 . This is because base62 is the name of your module , which is not a callable thing. "callable", as it sounds, means "able to be called".

The thing you want to call is the function defined in the module. The function is also named base62 , but the function is not available by that name, because right now that name represents the module.

Fortunately, functions in a module are represented as attributes of the module. Thus, you can access it as base62.base62 : the base62 attribute (the second one in that syntax) of whatever base62 (first one) is. In this case, the base62 function in the base62 module.

Alternately, you can import only that function from the module, by telling Python what to import from the module. That is written naturally, but not in the order you might expect: you write it as from base62 import base62 . (Again, the module name -what you're importing from - comes first, and the function name - what's being imported from it - second.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM