Skip to content

Instantly share code, notes, and snippets.

@evandixon
Created December 15, 2020 21:10
Show Gist options
  • Save evandixon/8f1d2e1f8f93598debe20ad2d385a971 to your computer and use it in GitHub Desktop.
Save evandixon/8f1d2e1f8f93598debe20ad2d385a971 to your computer and use it in GitHub Desktop.
Python's "There we did it" syntax - Things I shouldn't be able to do
#Class creation is a hack
class iShouldntBeAbleToDoAnyOfThis:
def __new__(self):
#Think long and hard before doing this in reality
return theRealClass()
def doTheThing(self):
print("Do the thing")
class theRealClass():
def doTheThing(self):
print("wrong thing")
#Property syntax is an afterthought
#I miss C# where I can just say "public string myProperty { get; set; }"
class propertyExample1:
def getter(self):
return self.property
def setter(iDontHaveToCallThisParameterSelf, value):
iDontHaveToCallThisParameterSelf.property = value
myProperty = property(getter, setter)
#And there's multiple ways of making properties, both wordier than C#
class propertyExample2:
@property
def myProperty(me):
return me.value
@myProperty.setter #Because we can't be consistent or anything
def myProperty(this, value):
this.value = value
theClass = iShouldntBeAbleToDoAnyOfThis()
theClass.doTheThing()
propertyExample1 = propertyExample1()
#whoops I just broke my ability to create more of this class
propertyExample1.myProperty = "test"
print(propertyExample1.myProperty)
propertyExample2 = propertyExample2()
propertyExample2.myProperty = "test2"
print(propertyExample2.myProperty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment