Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python Object
Search
Stephen Finucane
July 12, 2016
Technology
0
33
Python Object
How objects work in Python. A talk I gave to OSIC members, October 2015.
Stephen Finucane
July 12, 2016
Tweet
Share
More Decks by Stephen Finucane
See All by Stephen Finucane
API Contracts: Bringing OpenAPI and typing to OpenStack
stephenfin
0
170
Zero-downtime upgrades with SQLAlchemy + Alembic
stephenfin
0
780
OpenStack from 10,000ft
stephenfin
0
690
Teaching padawans to chop wood and carry water in their open source journey
stephenfin
0
270
What is Nova?
stephenfin
0
470
A Documentation-Driven Approach to Building APIs
stephenfin
0
220
A Lion, a Head, and a Dash of YAML (PyCon Limerick 2020)
stephenfin
0
360
Will Someone *Please* Tell Me What's Going On?
stephenfin
1
310
Trading Flexibility for Performance: The HPC Story in OpenStack
stephenfin
0
390
Other Decks in Technology
See All in Technology
Introduction to Sansan Meishi Maker Development Engineer
sansan33
PRO
0
310
能登半島災害現場エンジニアクロストーク 【JAWS FESTA 2025 in 金沢】
ditccsugii
0
630
Introduction to Bill One Development Engineer
sansan33
PRO
0
300
『OCI で学ぶクラウドネイティブ 実践 × 理論ガイド』 書籍概要
oracle4engineer
PRO
3
220
業務効率化をさらに加速させる、ノーコードツールとStep Functionsのハイブリッド化
smt7174
2
140
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
8.8k
AI Agent Dojo #2 watsonx Orchestrateフローの作成
oniak3ibm
PRO
0
120
小学4年生夏休みの自由研究「ぼくと Copilot エージェント」
taichinakamura
0
700
能登半島地震で見えた災害対応の課題と組織変革の重要性
ditccsugii
0
750
ガバメントクラウド(AWS)へのデータ移行戦略の立て方【虎の巻】 / 20251011 Mitsutosi Matsuo
shift_evolve
PRO
2
200
いまからでも遅くない!SSL/TLS証明書超入門(It's not too late to start! SSL/TLS Certificates: The Absolute Beginner's Guide)
norimuraz
0
230
衛星画像超解像化によって実現する2D, 3D空間情報の即時生成と“AI as a Service”/ Real-time generation spatial data enabled_by satellite image super-resolution
lehupa
0
160
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
33
2.3k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.9k
The Invisible Side of Design
smashingmag
302
51k
Keith and Marios Guide to Fast Websites
keithpitt
411
23k
What's in a price? How to price your products and services
michaelherold
246
12k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3k
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
Building Better People: How to give real-time feedback that sticks.
wjessup
369
20k
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.9k
Transcript
Stephen Finucane Software Developer, OpenStack Team Intel Shannon
What is Object Orientated design, and why do we care?
What is an object?
Know Java, and know Python is not Java These slides
compare the two, assuming Java knowledge for fair comparison. Don’t try to write Python like Java, though.
None
class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing
stuff') >> instance = MyClass() >> instance.do_stuff() doing stuff classes.py: An example of classes in Python
class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing
stuff')
object is Python being explicit You don’t need it in
Python 2, but you should use it to get “new style classes”
self is also Python being explicit Think of it like
the ‘this’ keyword in Java
public class MyClass { String name; ... public void setName(String
name) { this.name = name; } } MyClass.java: The ‘this’ keyword
class MyClass(object): name = None ... def set_name(self, name): self.name
= name self._example.py: The ‘self’ keyword
None
public class MyClass { public MyClass(String name) { ... }
public MyClass(String name, int age) { ... } } MyClass.java: Constructors in Java
class MyClass(object): def __init__(name, age=None): ... constructors.py: Constructors in Python
public class MyClass { public void display() { System.out.println("Hello, world!");
} } public class MySubClass extends MyClass { public void display() { System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
class MyClass(object) { def display(self): print('Hello world') class MySubClass(MyClass): def
display(self): print('Testing 123') constructors.py: Constructors in Python
public class MySubClass extends MyClass { public void display() {
super.display(); System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
class MySubClass(MyClass): def display(self): super(MySubClass, self).display(self) print('Testing 123') superclasses.py: Accessing
the Super Class
None
public interface Animal { public void eat(); public void travel();
} public class Mammal implements Animal { public void eat() { System.out.println("Mammal eats"); ... Animal.java, Mammal.java: Interfaces in Java
class Mammal: def eat(self): print('Mammal eats') ... class Reptile: def
eat(self): print('Reptile eats') ... animals.py: Duck Typing in Python
None
>> stephens_boat = Boat(...) >> ankurs_boat = Boat(...) >> stephens_boat
> ankurs_boat True The data model in practice
class Boat(object): """Model a boat.""" length = 0 color =
None def __init__(self, length, color): ... def __gt__(self, other): return self.length > other.length: boat.py: Using the data model
__lt__ - less than __le__ - less than or equal
__eq__ - equal __ne__ - not equal __gt__ - greater than __ge__ - greater than or equal __cmp__ - fallback, if above not implemented boat.py: Using the data model
>>> stephens_boat = Boat(100, 'red') >>> stephens_boat.__doc__ Model a boat.
>>> stephens_boat.__dict__ {'color': 'red', 'length': 100} >>> stephens_boat.__class__ <class '__main__.Boat'> The data model in practice
None
class Person(object): weight_kg = 0 @property def weight_lb(self): return self.weight_kg
* 2.2 @weight_lb.setter def weight_lb(self, value): self.weight_kg = value / 2.2 boat.py: Using the data model
None
• https://docs.python.org/2/tutorial/classes.html • https://en.wikipedia.org/wiki/Duck_typing • https://stackoverflow.com/questions/222877/how-to-use-super- in-python • https://docs.python.org/2/reference/datamodel.html