Python basic error-prone problem

Source: Internet
Author: User

1. Enter what the following code:

class Person :     = 1    def__init__(self):        pass    def  getage (self):         Print (__name__= Person () p.getage ()

The result is:

__main__

2. What code do I need to add in order to run this code?

 class   A (object):  def  __init__   (self,a,b): self.
    __a  = a self.  __b  = b  def< /span> Myprint (self):  print ( "  a=   ", Self.__a ,  " b=  Span style= "COLOR: #800000" > ", self. __b)   A1  =a (10,20) 

The answer is:

classA (object):def __init__(self, A, b): Self.__a=a self.__b=bdefMyprint (self):Print ('a=', self.__a,'b=', self.__b)    def __call__(self, num):Print ('Call :', num + self.__a)

3. What does the following code enter?

classB (object):deffn (self):Print ('B fn')    def __init__(self):Print ("B INIT")  classA (object):deffn (self):Print ('A fn')     def __new__(cls,a):Print("NEW", a)ifA>10:                returnSuper (A, CLS).__new__(CLS)returnB ()def __init__(self,a):Print ("INIT", a) A1= A (5) A1.fn () A2=a (20) A2.fn ()

The answer is:

NEW 5A fn

4. Add code to run the following program and return the specified result

class A (object):     def __init__ (self,a,b):         = a        = b        print ('init')    def Mydefault (self):         Print ( ' default ')  = A (10,20) a1.fn1 () a1.fn2 () a1.fn3 ()

return Result:

Defaultdefaultdefault

The answer is: Add __getattr__ () Magic method

classA (object):def __init__(self,a,b): self.a1=a self.b1=bPrint('Init')    defMydefault (self):Print ('default')    def __getattr__(self,name):returnSelf.mydefault A1= A (10,20) a1.fn1 () a1.fn2 () a1.fn3 ( )

Extended:

classA (object):def __init__(self, A, b): Self.a1=a self.b1=bPrint('Init')    defMydefault (Self, *args):Print('Default:'+str (args[0]))def __getattr__(self, name):Print("Other fn:", name)returnself.mydefaulta1= A (10, 20) a1.fn1 (33) a1.fn2 ('Hello') A1.fn3 (10)

The output is:

initother fn:fn1default:fn:fn2default:helloother fn:fn3default:10

5. There are three modules in a package, mod1.py, mod2.py, mod3.py, but how to ensure that only mod1 and mod3 are imported when using the From demopack import * module.

Answer: Add the __init__.py file and add it in the file:

__all__ = ['mod1','mod3']

6. Write a function that receives an integer parameter n and returns a function that multiplies the function's arguments and N and returns the result.

def Mulby (num):     def GN (val):         return num * val     return  gn  = Mulby (7)print(ZW ( 9));

7. Parse the code below where it's slow

def strtest1 (num):    str='first'     for inch range (num):        str+ ="X"    return Str    

Answer: Python's str is an immutable object, and each iteration generates a new STR object to store the new string, the larger the NUM, the more STR objects are created, and the more memory is consumed.

8. The following code:

class A (object):     def Show (self):         Print ' Base Show ' class B (A):     def Show (self):         Print ' Derived show '  = B () obj.show ()

How to call the Show method of Class A.
Here's how:

obj.__class__ =

The __class__ method points to the class object, only assigns a value type A to him, and then calls the method show, but when it is done, remember to change it back.

Python basic error-prone problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.