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
34
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
Steps Down the OpenAPI Path
stephenfin
0
56
Manage your OpenStack resources from Kubernetes with ORC
stephenfin
0
47
API Contracts: Bringing OpenAPI and typing to OpenStack
stephenfin
0
210
Zero-downtime upgrades with SQLAlchemy + Alembic
stephenfin
0
850
OpenStack from 10,000ft
stephenfin
0
730
Teaching padawans to chop wood and carry water in their open source journey
stephenfin
0
290
What is Nova?
stephenfin
0
510
A Documentation-Driven Approach to Building APIs
stephenfin
0
240
A Lion, a Head, and a Dash of YAML (PyCon Limerick 2020)
stephenfin
0
390
Other Decks in Technology
See All in Technology
進化するBits AI SREと私と組織
nulabinc
PRO
0
160
[E2]CCoEはAI指揮官へ。Bedrock×MCPで構築するコスト・セキュリティ自律運用基盤
taku1418
0
160
[JAWSDAYS2026][D8]その起票、愛が足りてますか?AWSサポートを味方につける、技術的「ラブレター」の書き方
hirosys_
3
180
ナレッジワーク IT情報系キャリア研究セッション資料(情報処理学会 第88回全国大会 )
kworkdev
PRO
0
190
Abuse report だけじゃない。AWS から緊急連絡が来る状況とは?昨今の攻撃や被害の事例の紹介と備えておきたい考え方について
kazzpapa3
1
680
Scrumは歪む — 組織設計の原理原則
dashi
0
170
楽しく学ぼう!ネットワーク入門
shotashiratori
1
330
AI時代のSaaSとETL
shoe116
1
140
Evolution of Claude Code & How to use features
oikon48
1
610
AI実装による「レビューボトルネック」を解消する仕様駆動開発(SDD)/ ai-sdd-review-bottleneck
rakus_dev
0
130
社内レビューは機能しているのか
matsuba
0
130
OCHaCafe S11 #2 コンテナ時代の次の一手:Wasm 最前線
oracle4engineer
PRO
2
130
Featured
See All Featured
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
How to Talk to Developers About Accessibility
jct
2
150
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
190
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
990
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
390
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
140
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
180
Writing Fast Ruby
sferik
630
63k
A Modern Web Designer's Workflow
chriscoyier
698
190k
First, design no harm
axbom
PRO
2
1.1k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.7k
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