ラベル Python の投稿を表示しています。 すべての投稿を表示
ラベル Python の投稿を表示しています。 すべての投稿を表示

2018/09/25

Python class method return None

cat = Cat()
print(cat.sound())

-->
--miao--
None
が出力される。class's method will return None defaultly,that means method executed successfully!
詳しく↓

On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

Using return None

This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.
In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or so).
def get_mother(person):
    if is_human(person):
        return person.mother
    else:
        return None

Using return

This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even tho you don't need it that often.
We got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function cause we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an the alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.
def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()
Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

Using no return at all

This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.
In the following example, we set person's mother's name, and then the function exits after completing successfully.
def set_mother(person, mother):
    if is_human(person):
        person.mother = mother
Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

2018/09/24

Great! get source code in Python

Google BloggerのPythonLibを使って、Bloggerを操作したいですが、Docはどこにもない。。Great! get source code in Python
http = credentials.authorize(http = httplib2.Http())
service = build('blogger','v3',http=http)
posts = service.posts() 
#for x in inspect.getmembers(posts,inspect.ismethod or inspect.isfunction):
# print(x)
  print(inspect.getsource(posts.update))

Difference between Method and Function in Python

Python Method
  1. Method is called by its name, but it is associated to an object (dependent).
  2. A method is implicitly passed the object on which it is invoked.
  3. It may or may not return any data.
  4. A method can operate on the data (instance variables) that is contained by the corresponding class  
Functions
  1. Function is block of code that is also called by its name. (independent)
  2. The function can have different parameters or may not have any at all. If any data (parameters) are passed, they are passed explicitly.
  3. It may or may not return any data.
  4. Function does not deal with Class and its instance concept. 
Difference between method and function
  1. Simply, function and method both look similar as they perform in almost similar way, but the key difference is the concept of ‘Class and its Object‘.
  2. Functions can be called only by its name, as it is defined independently. But methods can’t be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.

2017/02/06

python

import os
print(os.getcwd())
=>install directory!

import math
dir(math)
=>to see that in math module

from math import ceil
print ceil(4.5)
=>not necessary to use math.

import tkinter
import tkinter.filedialog
tk = tkinter.Tk()
tk.withdraw()        # 小ウィンドウを表示しないようにする。
args = { “initialdir” : “c:/”,
“filetypes” : [(“テキストファイル”, “*.txt”)],
“title” : “テスト”
}
print (tkinter.filedialog.askopenfilename(**args))
input(“press enter”)
=>open the file


myword=u"あいうえお”
=>japanese words is unicode 2 bytes
myword=myword.decode('utf-8')
->read in utf,out in utf

def my_methord():

2014/11/07

msg: the python mysqldb module is required

まずは pip を入れる。

 # yum -y install python-setuptools
 # easy_install pip

必要な依存パッケージをインストールする。

 # yum -y install python-devel mysql-devel

 # pip install mysql-python
 # pip freeze | grep -i mysql
 MySQL-python==1.2.3

以上。

2014/10/03

pipをインストール

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --proxy="http://proxy.jp:8080"

2014/05/13

Python pip インストール エラー



※CentOS6.5

★Python Package Index(pip)とは
Python Package Index(以下、pip)とは、Pythonにおけるパッケージ管理システムで、rubyのgemやperlのcpanみたいなものです。
pipは同様のパッケージ管理システムである、『setuptools』の"easy_install"を置き換えます。
pipをインストールするには、まずはsetuptoolsをインストールする必要があります。

★yumインストールでエラーになった!
Searching for pip
Reading http://pypi.python.org/simple/pip/
Download error: [Errno -2] Name or service not known -- Some packages may not be found!

・setuptoolsを確認する
 yum info python-setuptools
 ==>パッケージの情報を確認する

・ setuptoolsを再インストールする
 sudo yum -y remove python-setuptools
そして、最新の setuptools を自分で取ってきてインストールする。 pip もそこから改めて入れる。 要するにクリーンインストールする。
$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | sudo python
$ sudo easy_install pip
==>エラーになる。。。 easy_installで

 ★直接pipのインストール

①次のサイトあたりでダウンロードURLを確認して、ダウンロード。
※参照先らしいサイト
https://pypi.python.org/pypi/pip
wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz

②解凍します。
    tar xvzf pip-1.3.1.tar.gz

③解凍したディレクトリ内に移動してインストール。
    cd pip-1.3.1
    sudo python setup.py install --record files.txt

すると、次のような感じで、pipが使えました…

★uninstall

cat files.txt | xargs rm -rf