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
20
Manage your OpenStack resources from Kubernetes with ORC
stephenfin
0
34
API Contracts: Bringing OpenAPI and typing to OpenStack
stephenfin
0
200
Zero-downtime upgrades with SQLAlchemy + Alembic
stephenfin
0
830
OpenStack from 10,000ft
stephenfin
0
720
Teaching padawans to chop wood and carry water in their open source journey
stephenfin
0
280
What is Nova?
stephenfin
0
500
A Documentation-Driven Approach to Building APIs
stephenfin
0
230
A Lion, a Head, and a Dash of YAML (PyCon Limerick 2020)
stephenfin
0
380
Other Decks in Technology
See All in Technology
Data Hubグループ 紹介資料
sansan33
PRO
0
2.5k
AI駆動開発ライフサイクル(AI-DLC)の始め方
ryansbcho79
0
290
小さく、早く、可能性を多産する。生成AIプロジェクト / prAIrie-dog
visional_engineering_and_design
0
320
2025年の医用画像AI/AI×medical_imaging_in_2025_generated_by_AI
tdys13
0
290
AIと融ける人間の冒険
pujisi
0
110
Authlete で実装する MCP OAuth 認可サーバー #CIMD の実装を添えて
watahani
0
390
Oracle Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
2
670
1万人を変え日本を変える!!多層構造型ふりかえりの大規模組織変革 / 20260108 Kazuki Mori
shift_evolve
PRO
5
660
ハッカソンから社内プロダクトへ AIエージェント ko☆shi 開発で学んだ4つの重要要素
leveragestech
0
540
田舎で20年スクラム(後編):一個人が企業で長期戦アジャイルに挑む意味
chinmo
1
980
「駆動」って言葉、なんかカッコイイ_Mitz
comucal
PRO
0
130
[PR] はじめてのデジタルアイデンティティという本を書きました
ritou
0
750
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
270
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
140
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
Statistics for Hackers
jakevdp
799
230k
GitHub's CSS Performance
jonrohan
1032
470k
Mobile First: as difficult as doing things right
swwweet
225
10k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
72
Building an army of robots
kneath
306
46k
Thoughts on Productivity
jonyablonski
73
5k
Believing is Seeing
oripsolob
0
19
A Soul's Torment
seathinner
1
2.1k
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