Python Study

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What will be the result of executing the following Python code? class Organization(object): __employees - [] google = Organization() google._employees.append('Erik')

An AttributionError is raised because there is no __employees attribute, but it is still accessible as _Organization_employees

What will be the output of the following Python code? class A(object): def calc(self): return 7 class B(object): def calc(self): return 6 class C(A, B): pass c = C() print c.calc()

7

What will be the result of executing the following Python code? map = 7 def func(): return map print func()

7, because map is defined in the global namespace which is searched after the local namespace.

What will be the result of executing the following Python code? import ramdom def func(type_ = 's'): if type_ == 's': return 'Mark' elif type_ == 'i': return random.randint(0, 1000) def dec(func, type_): x = 8 def wrapper() value = func(type_) if isinstance(value, int): return value * x elif isinstance(value, basestring): return "Hi' + value return wrapper print dec(func, 'i')

A SyntaxError will be raised at "dec(func,'i')()"

What will be the output of the following Python code? def func(): return 1, 2, 3 (a, b) = func()

A ValueError will be raised because there are more returned values than the variables.

What is true about generators?

Generators have a next(0 method. ?Generators must contain a yield statement ? Generators are iterators which create their elements on-the-fly.

What is the value of b? a = ['orange', 'apple', 'banana'] b = a a = ['tomato', 'cucumber', 'carrot']

['orange', 'apple', 'banana']

What will be the output of the following Python code? import copy class A (object): pass a = A() a.lst = [1, 2, 3] a.str = 'cats and dogs' b = copy.copy(a) a.lst.append(100) print b.lst print b.str

[1,2,3,100], cats and dogs'

What will be the result of the following code? class A(object): def__repr__(self): return 'instance of A' a = A() b = a del a print b

a is deleted but the object it referred to is not because b still refers to it.

What would be the output of the following Python code? class A: brothers = [] def __init__(self, name): self.name = name a = A('Richard') b = A('Elly') a.brothers.append('John') print a.name, a.brothers, b.name, b.brothers

'Richard',['John'], 'Elly', ['John']

What will be the output of the following Python code? import datetime class Human(object): name = None gender = None birthdate = None def __getattr__(self, name): if name == 'age': return datetime.datetime.now() - self.birthdate else : return None def_getattribute__(self,name): return object.__getattribute__(self, name) h = Human() h.birthdate = datetime.datetime(1984, 8, 20) h.age = 28 print h.age

28

What will happen when 'python main.py' is executed? (Assume all modules are found on PYTHONPATH) constraints.py from classes import Student DEFAULT_DATE_FORMAT - "%Y-%M-%d" GREETING_MESSAGE = "Welcome to the %s University" % {Student.UNIVERSITY,} classes.py from main import datefmt class Student: UNIVERSITY ='MIT' def __init__(self, name, date_string, *args): self.name = name self.birthdate = datefmt(date_string) main.py from datetime import datetime from constraints import DEFAULT_DATE_FORMAT def datefmt(date_string): return datetime.strptime(date_string, DEFAULT_DATE_FORMAT) s = Student('Mary', '1981-4-23')

An ImportError will be raised

We want to create the class "Human" with the attributes "name" and "gender". We want to restrict assignment of the "gender" attribute to only "male" or "female". For this purpose, we have overridden __setattr_-(self,name,value). What will be the output of the following Python code? class Human(object): def __strattr__(self, name, value): if name == 'gender': if value in ('male', 'female'): self.gender = value else: raise AttributeError('Gender can only be "male", or "female") h = Human() h.name = 'Mary" h.gender = 'female' print h.gender

An infinite recursion and RuntimeError will be called because it is calling the same function

Why cant lists be used as dictionary keys?

Because lists are mutable and therefore not hashable

what is wrong with the code MULTIPLIER = 8 a = lambda x, y: (x * MULTIPLIER)/ y print a (2, 3)

Nothing is wrong

Consider the following code snippits. What is true about them? 1st = [ 'I', 'am', 'Python, 'programmer'] S1: s = "" for x in 1st: s += x S2: s = "".join(1st)

S1 is less efficient because strings are immutable objects, so each iteration creates a new string object. S2 is more efficient because concatenation is done in one pass and every character.

What is true about the following statements? S1: l = [ i for i in xrange (100000)] S2 x = ( i for i in xrange (100000))

S2 creates a generator function, its elements are not stored in memory, they are generated when the x.next()method is called.

What is true about expressions and objects that can be used in a 'with' statement with expression[as var]: ...BODY...

The object that is the result of the 'expression' must have __enter__() and __exit__() methods. The result of the 'expression' must be a context manager ( an object that implements context management protocol)

What will be the output of the following Python code? class A: pass class B: pass a = A() b = B() print type(a) == type(b), type(a), type(b)

True, instance, instance


Kaugnay na mga set ng pag-aaral

Unit 3- Chapter 1: Personality Traits

View Set

cặp từ trái nghĩa tiếng thuỵ điển

View Set

Solving Quadratic Equations: Factoring Assignment

View Set

Unit 13: Types of Mortgages and Sources of Financing

View Set