Created
April 4, 2015 18:05
-
-
Save johndgiese/fcd02db7330f295e55fc to your computer and use it in GitHub Desktop.
Weird stuff about python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 10 | |
b = 10 | |
print(a is b) | |
a = 10000000 | |
b = 10000000 | |
print(a is b) | |
def func(a): | |
return a + 4 | |
print(func.__closure__) | |
def create_closure(add): | |
closed = add*add | |
def closure(b): | |
return a + closed | |
return closure | |
c = create_closure(10) | |
print(c.__closure__) | |
import numbers, collections | |
# Virtual Base Classes | |
isinstance(4, numbers.Integral) # True | |
numbers.Integral in int.__bases__ # False | |
isinstance(set(), collections.Set) # True | |
collections.Set in set.__bases__ # False | |
# Static method descriptor | |
def staticmethod_(func): | |
class Descriptor(object): | |
def __get__(self, instance, owner): | |
return func | |
return Descriptor() | |
class S: | |
@staticmethod | |
def pass_through(val): | |
return val | |
class SS: | |
@staticmethod_ | |
def pass_through(val): | |
return val | |
print(S().pass_through("PASSING")) | |
print(SS().pass_through("PASSING")) | |
# Class method descriptor | |
def classmethod_(func): | |
class Descriptor(object): | |
def __get__(self, instance, owner): | |
def closure(*args, **kwargs): | |
return func(owner, *args, **kwargs) | |
return closure | |
return Descriptor() | |
class C: | |
@classmethod | |
def return_class(cls): | |
return cls | |
class CC: | |
@staticmethod_ | |
def return_class(cls): | |
return cls | |
class C_(C): pass | |
class CC_(C): pass | |
print(C_().return_class()) | |
print(CC_().return_class()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment