Created
February 2, 2020 19:54
-
-
Save blude/a0ad29583d75776ef9226806d45b443d to your computer and use it in GitHub Desktop.
python examples - nested functions and classes
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
""" | |
Resources: | |
---------- | |
1. Nested functions https://stackabuse.com/python-nested-functions/ | |
2. Packing problem: https://en.wikipedia.org/wiki/Packing_problems | |
- Answer on SO: https://stackoverflow.com/questions/7392143/python-implementations-of-packing-algorithm | |
""" | |
""" | |
Example #1: nested function | |
""" | |
# Given a point | |
point_x = rg.Point3D(...) | |
def modify_vertex(point): | |
# Create the inner function | |
def offset(distance): | |
# ...and do something here | |
return rg.Point3D(point + distance) | |
return offset | |
point_a = modify_vertex(point_x) | |
point_b = modify_vertex(point_x) | |
point_a(100) | |
point_b(200) | |
""" | |
Example #2: Nested function with 2 parameters | |
""" | |
# Given a point in a list of points | |
point_n = point_list[i] | |
def origin(point_a): | |
point_a.Z += 10 | |
# Create the inner function | |
def triangulo(point_b, point_c): | |
return rg.Triangulo(pontoA, pontoB, pontoC) | |
return triangle | |
triangle = origin(point_n) | |
triangle(point_list[n - 1], point_list[n + 1]) | |
""" | |
Example #3: Class creation | |
""" | |
class Wall(object): | |
def __init__(self, width, height): | |
self.width = width | |
self.height = height | |
def increase(self, length): | |
self.width = self.width + length | |
room_wall = Wall(100, 200) | |
room_wall.increase(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment